2

I have the following three models structured around the premise of the Survey.

class Survey(models.Model):
   ...
   id = models.UUIDField(_('Id'), primary_key=True, default=uuid.uuid4, editable=False,)
   name = models.CharField(_('Name'), max_length=120, blank=True, unique=True)
   slug = models.SlugField(_('Slug'), max_length=120, blank=True, unique=True)
   description = models.TextField(_('Description'), blank=True)
   ...

Each Survey can have multiple questions SurveyQuestion:

class SurveyQuestion(models.Model):
    ...
    survey = models.ForeignKey('surveys.Survey', on_delete=models.CASCADE, null=True, blank=True,)

And each SurveyQuestion can have multiple answers SurveyQuestionAnswer:

class SurveyQuestionAnswer(models.Model):
    ...
    survey_question = models.ForeignKey('surveys.SurveyQuestion', on_delete=models.CASCADE, null=True, blank=True,)

For the sake of brevity, imagine my Survey serializers as being as simple as possible:

class SurveySerialializer(serializers.ModelSerializer):

    class Meta:
        model = Survey
        fields = ('__all__')

Effectively, what I have is the following:

class Survey(APIView):
    """
        Survey GET request endpoint: fetches Survey
    """
    permission_classes = User

    def get(self, request, survey_slug):

        survey = Survey.objects.get(slug=survey_slug)

        serializer = SurveySerializer(survey)

        response = get_hug_response(message='Organisation Active Survey Fetched Successfully', data=serializer.data)
        return Response(data=response, status=status.HTTP_200_OK)

But, as you could all probably tell, the corresponding surveys.get('slug') fetch only returns the fields in the Survey model. Ideally, I would like to have some sort of fetch for each SurveyQuestion, and within that nested the SurveyQuestionAnswers

Any pro-tips and pointers would be most appreciated.

I have tried a few things, that only throw errors. I'm struggling to know what this type of API relationship is called in DRF so I can't find appropriate example guides to base the same principles from...

Relevant versions:

Django==2.2.1
djangorestframework==3.9.3
Micheal J. Roberts
  • 3,735
  • 4
  • 37
  • 76

1 Answers1

3

Create two serializers, SurveyQuestionAnswerSerializer and SurveyQuestionSerializer

class SurveyQuestionAnswerSerializer(serializers.ModelSerializer):
    class Meta:
        model = SurveyQuestionAnswer
        fields = '__all__'


class SurveyQuestionSerializer(serializers.ModelSerializer):
    survey_questionanswers = SurveyQuestionAnswerSerializer(many=True, read_only=True, source="surveyquestionanswer_set")

    class Meta:
        model = SurveyQuestion
        fields = '__all__'


class SurveySerializer(serializers.ModelSerializer):
    survey_questions = SurveyQuestionSerializer(many=True, read_only=True, source="surveyquestion_set")

    class Meta:
        model = Survey
        fields = '__all__'

For more info,
1. What is related_name used for in Django?
2. DRF Serializer's source argument

JPG
  • 82,442
  • 19
  • 127
  • 206