Django error when migrating with default field function that queries database
<p>I'm trying to create a default field callable like this:</p>

<pre><code>from datetime import date, timedelta
from django.db import models

class Preferences(models.Model):
    expiration_days = models.IntegerField(default=30)
    ... other db settings here ...

class ImportantBusinessObject(models.Model):
    expiration_date = models.DateField(default=get_default_expiration_date)

def get_default_expiration_date():
    expiration_days = Preferences.objects.first().expiration_days
    return date.today() + timedelta(days=expiration_days)
</code></pre>

<p>The idea is that there is a set of global preferences stored in the db, which affect default values on other model objects.</p>

<p>Everything is fine and dandy until it's time to add another field to the <code>Preferences</code> class. Then, when building the db (while running tests, for example), the migration which adds the <code>expiration_date</code> field ends up executing before the new migration adding another preference field. During the call to <code>get_default_expiration_date()</code>, the Preferences object is the latest version, but the DB hasn't gotten the newest migration, and I get a <code>column preferences.new_preference does not exist</code> error.</p>

<p>What's the best way to solve this?</p>

<p>Right now I'm disabling migrations during tests as a workaround, but I'm wondering if there's another way.</p>

<p>I've tried grabbing the model from the app registry from within <code>get_default_expiration_date()</code>, however it's still the latest one, not the versioned one that you're given in <code>migrations.RunPython</code> functions.</p>

<p>I've also tried wrapping the line that gets the <code>Preferences</code> object in a try/except block, but the db ends up in a rollback state, and I can't continue.</p>

<p>Other bits:</p>

<ul>
<li>I'm using Django 1.8</li>
<li>In my actual app, the <code>Preferences</code> class is actually a db singleton using django-solo.</li>
</ul>

<p><strong>UPDATE</strong>: Clarifying the order of operations</p>

<p>Here is sort of a timeline of what's happening:</p>

<ol>
<li><code>preferences/migrations/initial_0001.py</code>: initial preferences, includes <code>expiration_days</code></li>
<li><code>business/migrations/initial_0001.py</code>: initial business object, depends on <code>preferences 0001</code></li>
<li><code>preferences/migrations/add_pref_0002.py</code>: add a new field to <code>Preferences</code></li>
</ol>

<p>As soon as I define the migration in step #3, things start to break because when running to add the migration from step #2, <code>get_default_expiration_date</code> is called which throws the error. From the comment thread below about updating dependencies, my point was that in order to do that, every time I add a new preferences migration, I need to update the dependencies of the previously defined <code>business/migrations/initial_0001.py</code> (and any others that behave similarly). Hopefully this clarifies the situation a bit.</p>