Django Restframework“尝试在序列化器“ QuestionSerializer”上获取“ section_type”字段的值时出现“ AttributeError”

时间:2019-03-08 12:47:41

标签: django api django-rest-framework django-serializer

这是我的模特。

class QuestionSection(models.Model):
    section = models.CharField(max_length = 100, null = True, blank = True)
    max_marks = models.IntegerField()

    def __str__(self):
        return self.section

class Question(models.Model):
    question_section = models.ForeignKey(QuestionSection, on_delete = models.CASCADE, related_name = 'questions')
    section_type = models.CharField(max_length = 10,)
    questions = models.CharField(max_length = 350, null = True, blank = True)
    image = models.CharField(max_length = 10, null = True, blank = True)
    no_of_images = models.IntegerField(null = True, blank = True)
    marks = models.IntegerField()
    shop_view = models.CharField(max_length = 30, null = True, blank = True, choices=(('critical', 'critical'), ('major', 'major')))
    what_to_look_for = models.CharField(max_length = 350, null = True, blank = True)

    def __str__(self):
        return "{}-{}".format(self.section_type, self.marks)

这是我的序列化器

class QuestionSerializer(serializers.ModelSerializer):  
    class Meta:
        model = Question
        fields = '__all__'

class QuestionSectionSerializer(serializers.ModelSerializer):
    questions = QuestionSerializer(read_only = True)
    class Meta:
        model = QuestionSection
        fields = '__all__'

我不知道该怎么做。该API将类似于QuestionSection字段,并且里面将存在Question字段。

0 个答案:

没有答案
相关问题