一尘不染

Django-循环模型导入问题

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

这是某种循环进口问题吗?我尝试使用一个懒惰的引用,但这似乎也不起作用!


阅读 581

收藏
2020-03-27

共1个答案

一尘不染

删除的导入,Theme并将模型名称用作字符串。

theme = models.ForeignKey('themes.Theme')
2020-03-27