Django模型中的外键

时间:2013-09-28 20:54:58

标签: python django

以下是我的情况:SubCategory具有Topic的外键,Topic具有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 个答案:

答案 0 :(得分:6)

Topic放在引号中:"Topic"

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

或将Topic类放在SubCategory

之上

答案 1 :(得分:0)

您的问题是在Subcategory之后声明了Topic,因此在定义Subcategory时它不可用。

Python没有部分类声明或前向声明。但是您可以使用 init 方法来实现类似的功能。有关详细信息,请参阅here

答案 2 :(得分: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='+')

答案 3 :(得分:-1)

您的问题有多种解决方案:您可以定义后引用,从SubCategoryTopicrelated_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='+')

    ...