Django - the problem of importing standard models

I really do not understand this, so if someone can explain how this works, I would really appreciate it. I have two applications: accounts and theme ... here is a list of my settings:

INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'accounts', 'themes', ) 

In accounts, I'm trying to do this:

 from themes.models import Theme class Account(models.Model): ACTIVE_STATUS = 1 DEACTIVE_STATUS = 2 ARCHIVE_STATUS = 3 STATUS_CHOICES = ( (ACTIVE_STATUS, ('Active')), (DEACTIVE_STATUS, ('Deactive')), (ARCHIVE_STATUS, ('Archived')), ) id = models.AutoField(primary_key=True) name = models.CharField(max_length=250) slug = models.SlugField(unique=True, verbose_name='URL Slug') status = models.IntegerField(choices=STATUS_CHOICES, default=ACTIVE_STATUS, max_length=1) owner = models.ForeignKey(User) enable_comments = models.BooleanField(default=True) theme = models.ForeignKey(Theme) date_created = models.DateTimeField(default=datetime.now) 

And in my theme model:

 class Theme(models.Model): id = models.AutoField(primary_key=True) name = models.CharField(max_length=250) slug = models.SlugField(unique=True, verbose_name='URL Slug') date_created = models.DateTimeField(default=datetime.now) class Stylesheet(models.Model): id = models.AutoField(primary_key=True) account = models.ForeignKey(Account) date_created = models.DateTimeField(default=datetime.now) content = models.TextField() 

Django throws the following error:

 from themes.models import Theme ImportError: cannot import name Theme 

Is this some kind of cyclical import problem? I tried using a lazy link, but that doesn't work either!

+74
django django-models
Dec 07 '10 at 16:31
source share
4 answers

Remove the Theme import and use the model name as the string instead.

 theme = models.ForeignKey('themes.Theme') 
+128
Dec 07 2018-10-12T00:
source share

Something that I did not mention, somewhere in sufficient detail is described how to correctly formulate a line inside ForeignKey when referring to a model in another application. This line should be app_label.model_name . And, very importantly, app_label is not the entire line in INSTALLED_APPS, but only its last component. Therefore, if your INSTALLED_APPS looks like this:

 INSTALLED_APPS = ( ... 'path.to.app1', 'another.path.to.app2' ) 

to include ForeignKey in the model in app2 in the model app1, you have to do:

 app2_themodel = ForeignKey('app2.TheModel') 

I spent quite a bit of time solving the cyclic import problem (so I couldn’t just from another.path.to.app2.models import TheModel ), before I came across this, google / SO didn’t help (all the examples had single-component application paths ) so hopefully this will help other django newbies.

+43
Aug 10 2018-11-11T00:
source share

Use the get_model function from django.db.models , which is designed for lazy import models.

 MyModel = get_model('app_name', 'ModelName') 

In your case:

 Theme = get_model('themes', 'Theme') 

Now you can use Theme

+32
Jun 27 '14 at 9:25
source share

Since the correct way for Django 1.7 is 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

+22
Mar 25 '16 at 12:56
source share