序列化外键的外键(DRF)

时间:2019-02-05 20:25:18

标签: django django-rest-framework

我有3个级别的模型: 问题会议(包含一堆问题),问答。 它们都是通过ForeignKey连接的:

class QuestionSession(models.Model):
    name = models.CharField(max_length=100)

class Question(models.Model):
    description = models.CharField(max_length=400)
    question_type = models.CharField(max_length=50)
    answers = models.PositiveIntegerField(default=0)
    answers_to_close = models.PositiveIntegerField(default=3)
    answered = models.BooleanField(default=False)
    choices = models.CharField(
        max_length=1000, blank=True, null=True, default=None)

    question_session = models.ForeignKey(
        QuestionSession,
        on_delete=models.CASCADE,
        related_name='questions',
        blank=True,
        null=True,
        default=None
    )

class Answer(models.Model):
    question = models.ForeignKey(
        Question,
        related_name='answers_list',
        on_delete=models.CASCADE)
    answer = models.CharField(max_length=500)
    created_at = models.DateTimeField(auto_now_add=True)

还有我的serializers.py:

class QuestionSessionSerializer(serializers.ModelSerializer):
project = ProjectSerializer()

class Meta:
    model = QuestionSession
    fields = [
        'id',
        'questions',
        'project'
    ]


class QuestionSerializer(serializers.ModelSerializer):
    question_session = QuestionSessionSerializer()

class Meta:
    model = Question
    fields = [
        'id',
        'description',
        'question_type',
        'created_at',
        'answers',
        'answers_to_close',
        'answered',
        'question_session',
        'choices',
        'answers_list'
    ]


class AnswerSerializer(serializers.ModelSerializer):
    question = QuestionSerializer()

class Meta:
    model = Answer
    fields = [
        'question',
        'answer',
        'created_at'
    ]

我可以轻松地从QuestionSession中获取问题对象:

  

QuestionSession.objects.get(pk = 1).questions.all()

我还可以通过以下方式获得答案:

  

QuestionSession.objects.get(pk = 1).questions.objects.get(pk = 1).answers_list.all()

但是,当我通过Response发送此数据时,它仅发送答案的ID,而不是对象。我的看法:

    def get(self, request, **kwargs):
    """Fetch a single session"""

    session_id = kwargs['session_id']

    questions = QuestionSerializer(
        QuestionSession.objects.get(
            pk=session_id).questions.filter(answered=False),
        many=True
    )

    return Response({
        'session': session_id, 'questions': questions.data
    })

如何修改我的序列化器以传递Answer对象而不只是id?

2 个答案:

答案 0 :(得分:1)

您是否尝试在序列化程序上指定嵌套的深度

class QuestionSerializer(serializers.ModelSerializer):
    question_session = QuestionSessionSerializer()

    class Meta:
        model = Question
        fields = [
            'id',
            'description',
            'question_type',
            'created_at',
            'answers',
            'answers_to_close',
            'answered',
            'question_session',
            'choices',
            'answers_list'
        ]
        depth = 1

答案 1 :(得分:1)

使用深度将解决此问题,但请尝试使用prefetch_related和select_related。这将是最佳做法,并且不会减少查询(查询响应速度更快)。

对于您的问题,请使用select_related,它将解决该问题。有关更多参考或疑问,请使用以下链接:https://medium.com/quant-five/speed-up-django-nested-foreign-key-serializers-w-prefetch-related-ae7981719d3f

  

P.S:要检查使用Django Debug工具栏,它将说明多少   子查询在后台运行。