Django-원형 모델 가져 오기 문제
나는 이것을 정말로 얻지 못하고있다. 그래서 누군가 이것이 이것이 어떻게 작동하는지 설명 할 수 있다면 나는 그것을 매우 감사 할 것이다. 두 개의 응용 프로그램, 계정 및 테마가 있습니다. 여기에 내 설정 목록이 있습니다.
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'accounts',
'themes',
)
계정에서 이렇게하려고합니다.
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)
그리고 내 테마 모델에서 :
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에서 다음 오류가 발생합니다.
from themes.models import Theme
ImportError: cannot import name Theme
이것은 일종의 순환 수입 문제입니까? 게으른 참조를 사용해 보았지만 작동하지 않는 것 같습니다!
가져 오기를 제거하고 Theme
대신 모델 이름을 문자열로 사용하십시오.
theme = models.ForeignKey('themes.Theme')
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.
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')
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
참고URL : https://stackoverflow.com/questions/4379042/django-circular-model-import-issue
'Programing' 카테고리의 다른 글
UILabel에 윤곽선이있는 텍스트를 표시하려면 어떻게해야합니까? (0) | 2020.08.18 |
---|---|
CSS에서의 src 속성 정의 (0) | 2020.08.18 |
파이썬에서 문자열을 어떻게 분할하고 구문 분석 할 수 있습니까? (0) | 2020.08.17 |
MSBUILD : 오류 MSB1008 : 하나의 프로젝트 만 지정할 수 있습니다. (0) | 2020.08.17 |
사전 목록에 값이 이미 있는지 확인 하시겠습니까? (0) | 2020.08.17 |