Getting model ContentType in migration - Django 1.7
The answer is:
apps.get_model('contenttypes', 'ContentType')
:) Needed it myself today.
For Django 2.1 I had to import apps from global registry because passed apps into migration were instances of django.db.migrations.state.AppConfigStub
without populated models_module
attribute. And create_contenttypes
is checking this attribute.
from django.apps.registry import Apps, apps as global_appsfrom django.contrib.contenttypes.management import create_contenttypesfrom django.db import migrationsdef add_permision(apps: Apps, schema_editor): my_app_config = global_apps.get_app_config('my_app') create_contenttypes(my_app_config) ...
Having a similar issue when writing a data migration that spans several apps. Turns out Django only loads those models into the app registry that are affected by what the "dependencies" member of the migration states: https://code.djangoproject.com/ticket/24303
Had to basically add an entry to the migration dependencies that I use that is not directly related by e.g. a ForeignKey to the app that is currently being migrated.