Reputation: 2316
I have 3 levels of models: Question Session (contains a bunch of questions), Question and Answer. They all are connected via 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)
And my 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'
]
I can easily get question objects from QuestionSession with:
QuestionSession.objects.get(pk=1).questions.all()
And I'm also able to get answers with the following:
QuestionSession.objects.get(pk=1).questions.objects.get(pk=1).answers_list.all()
But when I send this data through Response, it only sends answers' ids instead of objects. My view:
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
})
How can I modify my serializers to pass Answer objects instead of just id?
Upvotes: 4
Views: 1939
Reputation: 1397
Using depth will solve the problem, but try to use prefetch_related and select_related. That will be best practice and it will reduce no queries(Query response is faster).
For your problem use select_related, it will solve the problem. For further reference or doubts use this link: https://medium.com/quant-five/speed-up-django-nested-foreign-key-serializers-w-prefetch-related-ae7981719d3f
P.S: For checking use Django Debug toolbar, it will explain how much subqueries are running in background.
Upvotes: 1
Reputation: 5450
Have you tried to specify nested depth on your serializer?:
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
Upvotes: 2