<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>muhuk.com &#187; xml</title>
	<atom:link href="http://www.muhuk.com/tag/xml/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.muhuk.com</link>
	<description>know thyself</description>
	<lastBuildDate>Thu, 29 Dec 2011 05:05:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Django Fixtures</title>
		<link>http://www.muhuk.com/2009/09/django-fixtures/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=django-fixtures</link>
		<comments>http://www.muhuk.com/2009/09/django-fixtures/#comments</comments>
		<pubDate>Thu, 24 Sep 2009 10:29:04 +0000</pubDate>
		<dc:creator>Atamert Ölçgen</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[fixtures]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[yml]]></category>

		<guid isPermaLink="false">http://www.muhuk.com/?p=295</guid>
		<description><![CDATA[A fixture is basically a data dump in a specific format. There is no restriction of which models or how much data a fixture can contain. Fixtures are portable. They work the same with all the database backends and operating systems supported by Django. This means that you can use fixtures as a simple data [...]<div><a class="addthis_button" href="//addthis.com/bookmark.php?v=250" addthis:url='http://www.muhuk.com/2009/09/django-fixtures/' addthis:title='Django Fixtures '><img src="//cache.addthis.com/cachefly/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a></div>]]></description>
			<content:encoded><![CDATA[<p>A <a href="http://docs.djangoproject.com/en/dev/howto/initial-data/#providing-initial-data-with-fixtures">fixture</a> is basically a data dump in a specific format. There is no restriction of which models or how much data a fixture can contain. Fixtures are portable. They work the same with all the database backends and operating systems supported by <a href="http://www.djangoproject.com">Django</a>. This means that you can use fixtures as a simple data migration tool<sup>1</sup>. Django supports a number of <a href="http://docs.djangoproject.com/en/dev/topics/serialization/">formats</a> by default. JSON format is widely used in the community. But you can easily create serializers for other formats. Although it is not a requirement, all built-in serializers produce <a href="http://en.wikipedia.org/wiki/Human-readable">human-readable</a> output. Therefore <strong>fixtures are an ideal way to keep your data and code together</strong> without tightly coupling them.</p>

<p>One limitation you should be aware of is that <em>fixtures are deserialized with the absolute values of primary keys</em>. This makes it difficult to store <a href="http://www.muhuk.com/2009/05/django-permission-system/">permissions in fixtures</a>. Also, say, you have two different fixtures for the same model with overlapping <code>pk</code> values. If you load both, instances with overlapping <code>pk</code>&#8216;s will be overwritten by the second load.</p>

<h3>Test Fixtures</h3>

<p>Fixtures are most useful during testing. They are an easy, reliable way to test your app with some data. If you prefer unittests like I do, here&#8217;s how to attach fixtures to your <code>TestCase</code>:</p>

<pre><span style="color:#555555">   1 </span><span style="color:#000000; font-weight:bold">from</span> django<span style="color:#000000">.</span>test <span style="color:#000000; font-weight:bold">import</span> TestCase
<span style="color:#555555">   2 </span>
<span style="color:#555555">   3 </span>
<span style="color:#555555">   4 </span><span style="color:#000000; font-weight:bold">class</span> <span style="color:#010181">MyTestCase</span><span style="color:#000000">(</span>TestCase<span style="color:#000000">):</span>
<span style="color:#555555">   5 </span>    fixtures <span style="color:#000000">= [</span><span style="color:#ff0000">'test_users'</span><span style="color:#000000">,</span> <span style="color:#ff0000">'test_foos.json'</span><span style="color:#000000">,</span> <span style="color:#ff0000">'test_bars.xml'</span><span style="color:#000000">]</span>
<span style="color:#555555">   6 </span>
<span style="color:#555555">   7 </span>    <span style="color:#838183; font-style:italic"># rest of your class</span></pre>

<p>Here is a tip for preparing fixtures quickly:</p>

<ol>
<li>Enable <code>admin</code> for your app. Just registering your models should be enough.</li>
<li>Create and/or edit test data via <code>admin</code>.</li>
<li>You can also use Django shell<sup>2</sup> for test data creation.</li>
<li>Dump your data as intermediary fixtures<sup>3</sup>.</li>
<li>Manually perform any final editing if necessary and save.</li>
</ol>

<p>This method might look like a lot of work. But it is actually very practical to prepare your test fixtures, or fixtures of any kind this way. Most of the work is removing unneeded data and tweaking the primary keys in the final step. But it is still faster than manually writing the whole thing.</p>

<h3>Initial Data</h3>

<p>If you want to load some data right after creating your database tables, you provide <a href="http://docs.djangoproject.com/en/dev/howto/initial-data/#providing-initial-data-with-fixtures"><code>initial_data</code></a> fixtures. At the end of <code>syncdb</code> (and <code>flush</code>), Django automatically loads fixtures in your installed apps<sup>4</sup> named <code>initial_data</code> regardless of their format.</p>

<p>This is good if your app require some data to function correctly. But there is a pitfall; as stated in Django documentation your <code>initial_data</code> <em>fixture will be loaded every time you run syncdb</em>. And if you edit these entries they will be overwritten next time you run <code>syncdb</code>. Therefore <strong><code>initial_data</code> is only good for data immutable in nature</strong>, such as units of length.</p>

<p>If you want to supply initial data for your project it is better to create one or more <code>bootstrap</code> fixtures and load them manually. Here is my minimal list of things I put in my <code>bootstrap</code> fixture:</p>

<ul>
<li>An <code>auth.User</code> as my superuser. It is much easier to load a superuser from fixtures than to create it interactively. I always run <code>syncdb</code> with <code>--noinput</code> and just change my superuser&#8217;s password once when I deploy.</li>
<li>A <a href="http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users">profile</a> for my superuser.</li>
<li>A <code>sites.site</code> object.</li>
</ul>

<p>And of course if all of the following fixtures will be loaded when you issue <code>manage.py loaddata bootstrap</code> command:</p>

<ul>
<li>project_dir/myapp1/fixtures/bootstrap.json</li>
<li>project_dir/myapp1/fixtures/bootstrap.xml</li>
<li>project_dir/myapp2/fixtures/bootstrap.json</li>
<li>project_dir/myapp3/fixtures/bootstrap.yml</li>
</ul>

<p>I think it would be nice if re-usable app authors agreed upon a convention like naming initial data fixtures <code>bootstrap</code>.</p>

<hr />

<p><strong>1</strong>: For very little amount of data this works fine. But serialization/deserialization becomes unreasonably slow for larger data sets. Then it&#8217;s better to use native tools for your database, modifying the input if necessarily.</p>

<p><strong>2</strong>: Use <code>manage.py shell</code> command to enter Django shell.</p>

<p><strong>3</strong>: Use <code>manage.py dumpdata &lt;app_name&gt; &gt; &lt;out_file&gt;</code> command to dump your app data as a fixture. By default Django uses JSON format. If you add <code>--indent=2</code> it will make the output much easier to read and edit.</p>

<p><strong>4</strong>: Apps that are in your <code>settings.INSTALLED_APPS</code>.</p>
<div><a class="addthis_button" href="http://www.muhuk.com//addthis.com/bookmark.php?v=250" addthis:url='http://www.muhuk.com/2009/09/django-fixtures/' addthis:title='Django Fixtures '><img src="//cache.addthis.com/cachefly/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a></div><p>Related posts:<ol>
<li><a href='http://www.muhuk.com/2011/11/working-with-files-in-django/' rel='bookmark' title='Working with files in Django &#8211; Part 1'>Working with files in Django &#8211; Part 1</a></li>
<li><a href='http://www.muhuk.com/2011/11/working-with-files-in-django-part-2/' rel='bookmark' title='Working with files in Django &#8211; Part 2'>Working with files in Django &#8211; Part 2</a></li>
<li><a href='http://www.muhuk.com/2011/11/working-with-files-in-django-part-3/' rel='bookmark' title='Working with files in Django &#8211; Part 3'>Working with files in Django &#8211; Part 3</a></li>
</ol></p> <p><a href="http://www.muhuk.com/?flattrss_redirect&amp;id=295&amp;md5=0dd30b00b2ec8275ffd012c60bd80608" title="Flattr" target="_blank"><img src="http://www.muhuk.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.muhuk.com/2009/09/django-fixtures/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="http://www.muhuk.com/?flattrss_redirect&amp;id=295&amp;md5=0dd30b00b2ec8275ffd012c60bd80608" type="text/html" />
	</item>
	</channel>
</rss>

