Django无法赋值,必须是另一个实例

时间:2018-01-19 11:51:41

标签: python django

所以我只是想在我的Lecture模型中添加LectureCategory,我希望用户只能在Classes或Seminars之间进行选择。如果我在两个模型中都有选择,我可以在django admin上看到它们,但是我收到错误:

Cannot assign "'0'": "Lecture.lecture_category" must be a "LectureCategory" instance.

如果我不在第二个模型中选择,那么在管理面板中将显示0或1,而不是我的值。有什么建议吗?

class LectureCategory(models.Model):
    lecture_category = models.IntegerField(choices=((0, "Classes "),
                                                    (1, "Seminars"),
                                                    ))

    def __str__(self):
        return str(self.lecture_category)


class Lecture(models.Model):
    course = models.ForeignKey('Course', on_delete=models.CASCADE, default='', related_name='lectures', null=True, )
    lecture_category = models.ForeignKey('LectureCategory', on_delete=models.CASCADE,
                                         default='', related_name='categories',
                                         choices=((0, "Classes "),
                                                  (1, "Seminars"),
                                                  )
                                         )

2 个答案:

答案 0 :(得分:2)

您绝对不需要LectureCategory模型来过滤类别上的Lecture queryeset:

class Lecture(models.Model):
    course = models.ForeignKey(
       'Course', 
        on_delete=models.CASCADE, 
        default=None, 
        related_name='lectures', 
        null=True, 
        )

    CATEGORY_CLASSES = 0
    CATEGORY_SEMINARS = 1
    CATEGORY_CHOICES = (
        (CATEGORY_CLASSES, "Classes"),
        (CATEGORY_SEMINARS, "Seminars"),
        )
    category = models.IntegerField(
       choices=CATEGORY_CHOICES
       )


# select only classes
Lecture.objects.filter(category=Lecture.CATEGORY_CLASSES)

# select only seminars
Lecture.objects.filter(category=Lecture.CATEGORY_SEMINARS)

# display the lecture's category readable label
# cf https://docs.djangoproject.com/en/2.0/ref/models/instances/#django.db.models.Model.get_FOO_display
print(lecture.get_category_display())

您也可以use custom managers here直接拥有Lecture.seminars.all()Lecture.classes.all()

如果您希望允许管理员添加新类别,那么拥有一个独特的LectureCategory模型是有意义的,但是您会在每个类别中放弃自定义管理器,实际上需要事先知道类别的任何内容。在这种情况下,您的LectureCategory模型需要一些label字段:

class LectureCategory(models.Model):
    label = models.CharField(
        "label",
         max_length=50
         )
    def __str__(self):
        return self.label


class Lecture(models.Model):
    course = models.ForeignKey(
       'Course', 
        on_delete=models.CASCADE, 
        default=None, 
        related_name='lectures', 
        null=True, 
        )

    category = models.ForeignKey(
        LectureCategory, 
        related_name="lectures"
        on_delete=models.PROTECT, 
        )

然后,如果你想迭代类别/讲座:

for category in Category.objects.all():
    print(category)
    for lecture in category.lectures.all():
        print(lecture)

答案 1 :(得分:0)

在您想要添加有关类别的更多信息之前,您不需要单独的类别模型。你可以简单地这样做

class Lecture(models.Model):
    Classes= 0
    Seminars= 1
    lecture_choice = (
        (Classes, 'Classes'),
        (Seminars, 'Seminars'),
    )
    course = models.ForeignKey('Course', on_delete=models.CASCADE, default='', related_name='lectures', null=True, )
    lecture_category = models.IntegerField(choices=lecture_choice ,default=Classes)