Prepopulate database with fixtures or with script?
<p>I'm not an expert but I think is a good idea using a Class to define choices and to prepopulate the database with these choices. I think that make easier to change choices, etc</p>

<p>So in my <code>models.py</code> I have:</p>

<pre><code>class City(models.Model):
    name = models.CharField(max_length=32)
    distance = models.SmallIntegerField(blank=True, null=True)
    #etc

class OtherClass(models.Model):
    name = models.CharField(max_length=32)
    #etc

class UserProfile(models.Model):
    name = models.CharField(max_length=32)
    city = models.ForeignKey(City)
    otherfield = models.ForeignKey(OtherClass)
    #etc
</code></pre>

<p><code>UserProfile</code> is what the users compile, <code>City</code>, <code>OtherClass</code> is where the programmer puts the options.</p>

<p>After the migration I have to create some <code>City</code> and <code>OtherClass</code> objects: they will be the options (and yes they have to be fixed).</p>

<p>I Just find out about the <a href="https://docs.djangoproject.com/en/1.9/howto/initial-data/" rel="nofollow noreferrer">fixtures</a>. Until now I was using a <code>script</code>:</p>

<pre><code>import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'sitopossedimenti.settings')

import django
django.setup()

from core.models import *

def populate():
    namecity1 = add_source('city1', None)
    namecity2 = add_source('city2', None)
    @etc

    nameotherclass1 = add_otherclass('name1', #etc)

    #etc some thousands more

def add_source(name, distance):
    s = model.Source.objects.get_or_create(name=name, distance=distance)[0]
    s.save()
    return s

def add_otherclass:
    #etc

if __name__ == '__main__':
    print ("Starting myapp population script...")
    populate()
</code></pre>

<p>For now the script works (about) and I'm afraid to change... but what do you think? Are the fixtures better? Why? There're differences?</p>