[Django]-How to choose specific columns when using depth in serializer

10👍

Try using nested serializers. Documentation here.

Example:

class FKOneSerializer(serializers.ModelSerializer):
    class Meta:
        model = FKOne
        fields = ('name', 'id')

class SomeSerializer(serializers.ModelSerializer):
    fk_one = FKOneSerializer()

    class Meta:
        model = MyAwesomeModel
        fields = ('id', 'fk_one','fk_two')

EDIT:

Similar answer here by the creator of the Django Rest Framework. Also includes some related notes, including that nested serializers are read-only and that you may need to include a source argument on the serializer field.

👤Alex

Leave a comment