320👍
Remove the import of Theme
and use the model name as a string instead.
theme = models.ForeignKey('themes.Theme')
87👍
Upto Django 1.7:
Use get_model
function from django.db.models
which is designed for lazy model imports.:
from django.db.models import get_model
MyModel = get_model('app_name', 'ModelName')
In your case:
from django.db.models import get_model
Theme = get_model('themes', 'Theme')
Now you can use Theme
For Django 1.7+:
from django.apps import apps
apps.get_model('app_label.model_name')
- [Django]-How do you change the collation type for a MySQL column?
- [Django]-Django values_list vs values
- [Django]-How do I reference a Django settings variable in my models.py?
59👍
Something I haven’t seen mentioned anywhere in sufficient detail is how to properly formulate the string inside ForeignKey when referencing a model in a different app. This string needs to be app_label.model_name
. And, very importantly, the app_label
is not the entire line in INSTALLED_APPS, but only the last component of it. So if your INSTALLED_APPS looks like this:
INSTALLED_APPS = (
...
'path.to.app1',
'another.path.to.app2'
)
then to include a ForeignKey to a model in app2 in an app1 model, you must do:
app2_themodel = ForeignKey('app2.TheModel')
I spent quite a long time trying to solve a circular import issue (so I couldn’t just from another.path.to.app2.models import TheModel
) before I stumbled onto this, google/SO was no help (all the examples had single component app paths), so hopefully this will help other django newbies.
- [Django]-Troubleshooting Site Slowness on a Nginx + Gunicorn + Django Stack
- [Django]-PHP Frameworks (CodeIgniter, Yii, CakePHP) vs. Django
- [Django]-Automatic creation date for Django model form objects
52👍
Since Django 1.7 correct way is to go like this:
from django.apps import apps
YourModel = apps.get_model('your_app_name', 'YourModel')
See: https://docs.djangoproject.com/ja/1.9/ref/applications/#django.apps.apps.get_model
- [Django]-Running a specific test case in Django when your app has a tests directory
- [Django]-Laravel's dd() equivalent in django
- [Django]-What's the difference between CharField and TextField in Django?
0👍
I had the same problem but on using MyModel = get_model('app_name', 'ModelName')
I got a new error 'raise AppRegistryNotReady("Models aren't loaded yet.")'
I tried all the other methods on this page but none worked for me.
The way I fixed it was to use:
MyModel = get_model('app_name', 'ModelName')
but actually in the class rather than outside of it.
- [Django]-What does error mean? : "Forbidden (Referer checking failed – no Referer.):"
- [Django]-Trying to migrate in Django 1.9 — strange SQL error "django.db.utils.OperationalError: near ")": syntax error"
- [Django]-How to filter empty or NULL names in a QuerySet?
-2👍
This is how I fix the circular issue by specifying ‘app_name.model_name’ and TYPE_CHECKING
profile app's models.py
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from team.models import Team
team = models.ForeignKey('team.Team',...)
team app's models.py
Class Team
name = models.CharField(...)
head = models.ForeignKey('profile.Profile',...)
- [Django]-Can't connect to local MySQL server through socket '/tmp/mysql.sock
- [Django]-Using Cloudfront with Django S3Boto
- [Django]-Django ignores router when running tests?