Django - Populating a database for test purposes

Check this app

https://github.com/aerosol/django-dilla/

Let's say you wrote your blog application (oh yeah, your favorite!) in Django. Unit tests went fine, and everything runs extremely fast, even those ORM-generated ultra-long queries. You've added several categorized posts and it's still stable as a rock. You're quite sure the app is efficient and ready to for live deployment. Right? Wrong.


You can use fixtures for this purpose, and the loaddata management command.

One approach is to do it like this.

  1. Prepare your test database.

  2. Use dumpdata to create JSON export of the database.

  3. Put this in the fixtures directory of your application.

  4. Write your unit tests to load this "fixture": https://docs.djangoproject.com/en/2.2/topics/testing/tools/#django.test.TransactionTestCase.fixtures


Django fixtures provide a mechanism for importing data on syncdb. However, doing this initial data propagation is often easier via Python code. The technique you outline should work, either via syncdb or a management command. For instance, via syncdb, in my_app/management.py:

def init_data(sender, **kwargs):
    for i in range(1000):
        MyModel(number=i).save()

signals.post_syncdb.connect(init_data)

Or, in a management command in myapp/management/commands/my_command.py:

from django.core.management.base import BaseCommand, CommandError

from models import MyModel

class MyCommand(BaseCommand):
    def handle(self, *args, **options):
        if len(args) > 0:
            raise CommandError('need exactly zero arguments')

        for i in range(1000):
            MyModel(number=i).save()

You can then export this data to a fixture, or continue importing using the management command. If you choose to continue to use the syncdb signal, you'll want to conditionally run the init_data function to prevent the data getting imported on subsequent syncdb calls. When a fixture isn't sufficient, I personally like to do both: create a management command to import data, but have the first syncdb invocation do the import automatically. That way, deployment is more automated but I can still easily make modifications to the initial data and re-run the import.