I have two models
class Answer(models.Model):
answer = models.TextField()
created_at = models.DateTimeField(editable=False, default=timezone.now)
updated_at = models.DateTimeField(default=timezone.now)
user = models.ForeignKey('users.CustomUser', on_delete=models.PROTECT)
question = models.ForeignKey('Question', on_delete=models.PROTECT)
number_of_points = models.IntegerField(default=0)
moderate_status = models.BooleanField(default=False)
and
class Question(models.Model):
question_subject = models.TextField()
question_text = models.TextField(default=None, null=True, blank=True)
slug = models.SlugField(max_length=128, unique=True, null=False, editable=False)
created_at = models.DateTimeField(editable=False, default=timezone.now)
updated_at = models.DateTimeField(default=timezone.now)
user = models.ForeignKey('users.CustomUser', on_delete=models.PROTECT)
animal = models.ForeignKey('animals.Animal', on_delete=models.PROTECT)
serializers.py
class QuestionDetailSerializer(serializers.ModelSerializer):
answers = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
class Meta:
model = Question
fields = '__all__'
views.py
class QuestionsDetailView(generics.ListAPIView):
queryset = Question.objects.all()
serializer_class = QuestionsSerializer
def get_queryset(self):
return super().get_queryset().filter(
id=self.kwargs['pk']
)
url.py
path('questions/<int:pk>', QuestionsDetailView.as_view()),
And i want to combine 2 queryset, one being already that filters the question by pk provided in the url, and the other queryset i'd like to give is Answer.objects.all().filter(question__id='pk'). Essentially i want to show all the questions with answers to a particular question.