4

I have serialised a model into JSON that has foreign keys. The API will show the title of the foreign key but will not nest the data that belongs to that foreign key. How do I do this.

serializers.py

class ReportFieldSerializers(serializers.ModelSerializer):
    form = serializers.RelatedField()
    class Meta:
        model = ReportField
        fields = (
            'id',
            'title',
            'form'
        )

api.py

class ReportFieldList(APIView):
    def get(self, request, format=None):
        report_field = ReportField.objects.all()
        serialized_report_field = ReportFieldSerializers(report_field, many=True)
        return Response(serialized_report_field.data)

class ReportFieldDetail(APIView):
    def get_object(self, pk):
        try:
            return ReportField.objects.get(pk=pk)
        except ReportField.DoesNotExist:
            raise Http404

    def get(self, request, pk, format=None):
        report_field = self.get_object(pk)
        serialized_report_field = ReportFieldSerializers(report_field)
        return Response(serialized_report_field.data)

models.py

class Report(models.Model):
    title = models.CharField()
    form = models.ForeignKey()

class Form(models.Model):
    # Form details
1
  • You have ReportField in your serializers.py, but a model Report in your models.py. Can you please correct that to reflect your actual code. Commented Nov 3, 2014 at 19:43

1 Answer 1

7

There is an option depth, that can be used in the Meta class of the serializer. For example:

class ReportFieldSerializers(serializers.ModelSerializer):

    class Meta:
        model = ReportField
        fields = (
            'id',
            'title',
            'form'
        )
        depth = 1

This will go one step down in the related models. As you see you don't need the RelatedField. If, let's say, there would be a third model class, for example:

class Form(models.Model):
    example = ForeignKey('AnotherModel')

class AnotherModel(models.Model):
    # model fields

you could also use depth=2 in your ReportFieldSerializer to display the information for this model.

I assume that in your model Report the field form shows to Form: models.ForeignKey(Form).

I answered already a similar question here: Django rest_framework serializer with inner relationship

Sign up to request clarification or add additional context in comments.

1 Comment

This helped me. Is it possible to remove the id field of the foreign key altogether? Or am looking at it the wrong way? I'm new to django, I was thinking it defeats the purpose of hiding the id field on the foreign model's serializer if it will be just displayed as well if taken from the other object. Is there a reason that i'm missing? Is it perhaps for accessing?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.