'Django data migration with contenttype keys

I need to make a Django data migration that creates some predefined initial values in tables.

To create the predefined values I am using a fixture file that is called within the migration like so:

def run_data_populating_fixtures(apps, schema_editor):
    call_command('loaddata', 'apps/app/fixtures/test.json')


class Migration(migrations.Migration):
    dependencies = [
        ('app', '0012'),
        ('contenttypes', '0001_initial'),
        ('core', '0001_initial')
    ]
    operations = [
        migrations.RunPython(run_data_populating_fixtures, elidable=False)
    ]

The table I am populating has a column content_type_id which is a foreign key to the django_content_type table. This becomes a problem because at the point when the data migration executes, the table doesn't have any of my apps' models registered. Basically in the fixture there is an explicit key specified as "content_type": 11, and it causes a FK violation.

How can I run my data migration after django_content_type table has my models registered?

I tried using natural keys instead of explicit ones as per migration article, like so

"content_type": ["core", "mymodel"]

but it doesn't seem to help, as it just produces an error that it couldn't find such content type.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source