I have these 2 serializers:
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: model = MyModel fields = ('lengthy', 'touple', 'of', \ 'fields', 'like', '10', 'of', 'them', 'additional_field')
This is obviously rather ugly code. I would like to get and extend the fields
touple from super()
, however I have no idea how.
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',)
we can do like
class AdvandedSerializer(BasicSerializer): # ... some code class Meta: model = MyModel fields = BasicSerializer.Meta.fields + ('additional_field',)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With