6

这是我的情况:SubCategory有外键TopicTopic外键SubCategory

class SubCategory(models.Model):
    name = models.CharField(max_length=100)
    slug = models.SlugField(max_length=110)
    description = models.TextField(default='')
    ordering = models.PositiveIntegerField(default=1)
    category = models.ForeignKey(Category)
    created_on = models.DateTimeField(auto_now_add=True)
    created_by = models.ForeignKey(User)
    updated_on = models.DateTimeField(blank=True, null=True)
    updated_by = models.ForeignKey(User, related_name='+')
    num_topics = models.IntegerField(default=0)
    num_posts = models.IntegerField(default=0)
    last_topic = models.ForeignKey(Topic, related_name='+')


class Topic(models.Model):
    name = models.CharField(max_length=300)
    slug = models.SlugField(max_length=300)
    description = models.TextField(default='')
    subcategory = models.ForeignKey(SubCategory)
    created_on = models.DateTimeField(auto_now_add=True)
    created_by = models.ForeignKey(User)
    updated_on = models.DateTimeField(blank=True, null=True)
    updated_by = models.ForeignKey(User, related_name='+')

当我运行此代码时,它会给出以下错误:

NameError: name 'Topic' is not defined.

谁能告诉我如何解决它?

4

4 回答 4

10

要么加Topic引号:"Topic"

last_topic = models.ForeignKey("Topic", related_name='+')

或将班级置于班级Topic之上SubCategory

于 2013-09-28T20:57:49.677 回答
0

您的问题是主题是在子类别之后声明的,因此在定义子类别时它不可用。

Python 没有部分类声明或前向声明。但是你可以使用init方法来实现类似的东西。有关详细信息,请参见此处

于 2013-09-28T20:57:42.640 回答
0

尝试用引号将模型括起来:

class SubCategory(models.Model):
    name = models.CharField(max_length=100)
    slug = models.SlugField(max_length=110)
    description = models.TextField(default='')
    ordering = models.PositiveIntegerField(default=1)
    category = models.ForeignKey('Category')
    created_on = models.DateTimeField(auto_now_add=True)
    created_by = models.ForeignKey('User')
    updated_on = models.DateTimeField(blank=True, null=True)
    updated_by = models.ForeignKey('User', related_name='+')
    num_topics = models.IntegerField(default=0)
    num_posts = models.IntegerField(default=0)
    last_topic = models.ForeignKey('Topic', related_name='+')


class Topic(models.Model):
    name = models.CharField(max_length=300)
    slug = models.SlugField(max_length=300)
    description = models.TextField(default='')
    subcategory = models.ForeignKey("SubCategory")
    created_on = models.DateTimeField(auto_now_add=True)
    created_by = models.ForeignKey('User')
    updated_on = models.DateTimeField(blank=True, null=True)
    updated_by = models.ForeignKey('User', related_name='+')
于 2017-01-04T10:38:56.310 回答
-1

您的问题有几种解决方案:您可以定义一个反向引用,从SubCategoryTopic通过related_name ForeignKey属性,或者简单地将 ForeignKey 模型的名称作为字符串传递。

解决方案1:

class SubCategory(models.Model):
    name = models.CharField(max_length=100)
    slug = models.SlugField(max_length=110)
    description = models.TextField(default='')
    ordering = models.PositiveIntegerField(default=1)
    ...
    last_topic = models.ForeignKey(Topic, related_name='subcategory')

解决方案2:

class SubCategory(models.Model):
    name = models.CharField(max_length=100)
    slug = models.SlugField(max_length=110)
    description = models.TextField(default='')
    ordering = models.PositiveIntegerField(default=1)
    category = models.ForeignKey(Category)
    created_on = models.DateTimeField(auto_now_add=True)
    created_by = models.ForeignKey(User)
    updated_on = models.DateTimeField(blank=True, null=True)
    updated_by = models.ForeignKey(User, related_name='+')
    num_topics = models.IntegerField(default=0)
    num_posts = models.IntegerField(default=0)
    last_topic = models.ForeignKey("Topic", related_name='+')


class Topic(models.Model):
    name = models.CharField(max_length=300)
    slug = models.SlugField(max_length=300)
    description = models.TextField(default='')
    subcategory = models.ForeignKey(SubCategory)
    created_on = models.DateTimeField(auto_now_add=True)
    created_by = models.ForeignKey(User)
    updated_on = models.DateTimeField(blank=True, null=True)
    updated_by = models.ForeignKey(User, related_name='+')

    ...
于 2013-09-28T21:03:32.760 回答