[Django]-Django serializer inherit and extend fields

134👍

You can do:

class BasicSerializer(serializers.ModelSerializer):
    class Meta:
         model = MyModel
         fields = ('lengthy', 'touple', 'of', 'fields', 'like', '10', 'of', 'them')

class AdvandedSerializer(BasicSerializer):
    additional_field = serializers.SerializerMethodField()

    def get_additional_field(self, obj):
        return('not important')

    class Meta(BasicSerializer.Meta):
        fields = BasicSerializer.Meta.fields + ('additional_field',)

25👍

we can do like

class AdvandedSerializer(BasicSerializer):
    # ... some code

    class Meta:
         model = MyModel
         fields = BasicSerializer.Meta.fields + ('additional_field',)

Leave a comment