How to create per-project source_data in Django 1.7+

Prior to Django 1.7, I used the definition for each fixtures project in the settings:

 FIXTURE_DIRS = ('myproject/fixtures',) 

and use this to place the initial_data.json fixture for storing default default groups for the entire project. This worked well for me, as I could keep the design clean by dividing the data for each project from specific applications.

Now with the parameters of Django 1.7, initial_data deprecated, offering to enable data migrations along with application schema migrations; leaving no obvious choice for global source data for each project.

In addition, the new migration infrastructure sets all obsolete source data before performing migrations for compatible applications (including django.contrib.auth ). This leads to the fact that my device containing the default groups disables the installation , since the auth_group table is not yet present in the database.

Any suggestions on how to (elegantly) get the lights to start after all migrations, or at least after migrating applications to the device? Or any other ideas to solve this problem? I find fixtures a great way to provide source data and would like to have a simple and clean way to declare them for an unattended installation. The new RunPython is too cumbersome, and I find it redundant for most purposes; and apparently it is only available for application migration.

+8
django django-migrations django-fixtures
source share
2 answers

If you absolutely want to use lights, just use RunPython and call_command in your data migrations.

 from django.db import migrations from django.core.management import call_command def add_data(apps, schema_editor): call_command('loaddata', 'thefixture.json') def remove_data(apps, schema_editor): call_command('flush') class Migration(migrations.Migration): dependencies = [ ('roundtable', '0001_initial'), ] operations = [ migrations.RunPython( add_data, reverse_code=remove_data), ] 

However, it is recommended to load data using python and Django ORM code, since you won't have to deal with integrity issues.

Source

+4
source share

I recommend using factories instead of fixtures, they are messy and difficult to maintain, it is better to use FactoryBoy with Django.

+1
source share

All Articles