[Answered ]-Serializing Related Data in Django using Natural Foregin Keys (Three Models, Two Layers)

1👍

You can use nested model serializers for your purposes like this:

class ProductSerializer(serializers.ModelSerializer):
    class Meta:
        model = Product
        fields = "__all__"

class ProductReleaseSerializer(serializers.ModelSerializer):
    product = ProductSerializer(many=True)

    class Meta:
        model = ProductRelease
        fields = "__all__"


class ReleaseNoteSerializer(serializers.ModelSerializer):
    product_release = ProductReleaseSerializer()

    class Meta:
        model = ReleaseNote
        fields = "__all__"

release_note_model_instance = ReleaseNote.objects.all()
print(ReleaseNoteSerializer(release_note_model_instance, many=True).data)

Leave a comment