如何正确地将具有多个外键的表序列化到同一表

时间:2019-04-04 14:34:22

标签: python django serialization django-rest-framework

我正在尝试为测验创建一个API。但是,表数据未正确序列化。字段invalid_answers_ar发送的是递增数字,而不是实际数据。其他所有字段都可以正常工作

串行器正在部分工作。我正在运行Django 1.11

models.py

class Question(models.Model):
    question = models.CharField("Question",max_length=255,blank=True, null=True)
    question_ar = models.CharField("Question arabic",max_length=255,blank=True, null=True)
    correct_answer = models.CharField("Correct Answer",max_length=255,blank=True, null=True)
    correct_answer_ar = models.CharField("Correct Answer arabic",max_length=255,blank=True, null=True)
    def __str__(self):
        return (self.question)

class Incorrect(models.Model):
    incorrect_answers = models.CharField("Incorrect Answer",max_length=255,blank=True, null=True)
    incorrect_answers_ar = models.CharField("Incorrect Answer arabic",max_length=255,blank=True, null=True)
    question = models.ForeignKey(Question,related_name='incorrect_answers', on_delete=models.CASCADE,blank=True, null=True)
    question_ar = models.ForeignKey(Question,related_name='incorrect_answers_ar', on_delete=models.CASCADE,blank=True, null=True)
    def __str__(self):
        return (self.incorrect_answers)

admin.py

class IncorrectInline(admin.TabularInline):
    model = Incorrect
    fk_name = "question"

    class QuestionAdmin(admin.ModelAdmin):
    inlines = [
        IncorrectInline,
    ]

admin.site.register(Question, QuestionAdmin)

serializers.py

from .models import Question,Incorrect


class QuestionSerializer(serializers.ModelSerializer):
    incorrect_answers = serializers.StringRelatedField(many=True)
    class Meta:
        model = Question
        fields = ('question','question_ar', 'correct_answer','correct_answer_ar','incorrect_answers_ar','incorrect_answers')

views.py

class ListQuestionsView(generics.ListAPIView):
    queryset = Question.objects.all()
    serializer_class = QuestionSerializer

这就是我现在要得到的

[
{
    "question": "how old are you",
    "question_ar": "old arabic",
    "correct_answer": "21",
    "correct_answer_ar": "21 ar",
    "incorrect_answers_ar": [
        1,
        2,
        3
    ],
    "incorrect_answers": [
        "22",
        "23",
        "24"
    ]
},
{
    "question": "my mobile?",
    "question_ar": "my mobile arabic?",
    "correct_answer": "asus",
    "correct_answer_ar": "asus arabic",
    "incorrect_answers_ar": [
        4,
        5,
        6
    ],
    "incorrect_answers": [
        "redmi",
        "samsung",
        "lg"
    ]
},
{
    "question": "My bike",
    "question_ar": "my bike arabic?",
    "correct_answer": "gixxer",
    "correct_answer_ar": "gixxer arabic",
    "incorrect_answers_ar": [
        7,
        8,
        9
    ],
    "incorrect_answers": [
        "hornet",
        "fz",
        "apache"
    ]
},
{
    "question": "What the hell?",
    "question_ar": "What the hell? arabic",
    "correct_answer": "shit",
    "correct_answer_ar": "shit arabic",
    "incorrect_answers_ar": [
        10,
        11,
        12,
        13
    ],
    "incorrect_answers": [
        "good",
        "bad",
        "okay",
        "great"
    ]
}
]

如您所见,不正确的答案是数字而不是实际值。知道我在做什么错吗?

1 个答案:

答案 0 :(得分:2)

如果未在序列化程序中显式定义字段,则相关字段将使用其ID进行序列化。就像您对 incorrect_answers 所做的一样,您还需要在序列化程序中显式定义 incorrect_answers_ar 来使用字符串值。像这样:

class QuestionSerializer(serializers.ModelSerializer):
    incorrect_answers = serializers.StringRelatedField(many=True)
    incorrect_answers_ar = serializers.StringRelatedField(many=True)
    class Meta:
        model = Question
        fields = ('question','question_ar', 'correct_answer','correct_answer_ar','incorrect_answers_ar','incorrect_answers')