{"id":401,"date":"2012-09-30T20:53:50","date_gmt":"2012-10-01T00:53:50","guid":{"rendered":"http:\/\/dashdrum.com\/blog\/?p=401"},"modified":"2012-09-30T20:53:50","modified_gmt":"2012-10-01T00:53:50","slug":"quick-post-django-factory-boy","status":"publish","type":"post","link":"https:\/\/dashdrum.com\/blog\/2012\/09\/quick-post-django-factory-boy\/","title":{"rendered":"Quick Post &#8211; Django Factory Boy"},"content":{"rendered":"<p>I&#8217;ve been using <a href=\"https:\/\/github.com\/dnerdy\/factory_boy\">factoryboy<\/a> in my tests as of late.  It&#8217;s a nice tool to create test data in a programmatic way &#8211; no fixtures.<\/p>\n<p>A factory includes a Sequence class that can be used to create unique field values with each instance.  The examples I&#8217;ve seen use something like this:<\/p>\n<pre><code>title = factory.Sequence(lambda n: 'Event ' + n)\n<\/code><\/pre>\n<p>Which will create successive records with titles like &#8220;Event 0&#8221;, &#8220;Event 1&#8243;, Event 2&#8221;, etc.<\/p>\n<p>The variable <code>n<\/code> is a string type by default, but I wanted to use it as an integer &#8211; in this case to set a value to an Integer field:<\/p>\n<pre><code>capacity = factory.Sequence(lambda n: n * 10)\n<\/code><\/pre>\n<p>However, this instead put 10 ones in the field, instead of the number held in <code>n<\/code> times 10.<\/p>\n<p>A quick perusing of the documentation <a href=\"http:\/\/factoryboy.readthedocs.org\/en\/latest\/internals.html\">here<\/a> showed that the Sequence class takes a second parameter after the function that indicates the desired type for <code>n<\/code>.  Adding <code>int<\/code> to the end of the call makes it work much better.<\/p>\n<pre><code>capacity = factory.Sequence(lambda n: n * 10,int)\n<\/code><\/pre>\n<p>Now the field holds a multiple of 10.<\/p>\n<p>Want a more complicated example?  Here I assign a date field that is <code>n<\/code> nubmer of days after the current date.<\/p>\n<pre><code>event_date = factory.Sequence(lambda n:(datetime.now() + timedelta(days=n)).date(), int)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been using factoryboy in my tests as of late. It&#8217;s a nice tool to create test data in a programmatic way &#8211; no fixtures. A factory includes a Sequence class that can be used to create unique field values with each instance. The examples I&#8217;ve seen use something like this: title = factory.Sequence(lambda n: &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/dashdrum.com\/blog\/2012\/09\/quick-post-django-factory-boy\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Quick Post &#8211; Django Factory Boy&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-401","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/401","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/comments?post=401"}],"version-history":[{"count":1,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/401\/revisions"}],"predecessor-version":[{"id":402,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/401\/revisions\/402"}],"wp:attachment":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/media?parent=401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/categories?post=401"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/tags?post=401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}