Answer the question
In order to leave comments, you need to log in
How to make one function for all classes?
I have many classes that use the same function, but with a slight difference. How can I put it in a separate class so as not to repeat myself?
class AttrNameSerializer(serializers.ModelSerializer):
class Meta:
model = AttributeName
fields = '__all__'
def to_representation(self, instance):
result = super(AttrNameSerializer, self).to_representation(
instance)
return OrderedDict([(key, result[key]) for key in result if
result[key] is not None])
class ProductSerializer(serializers.ModelSerializer):
published_on = serializers.DateTimeField(format='iso-8601')
class Meta:
model = Product
fields = '__all__'
def to_representation(self, instance):
result = super(ProductSerializer, self).to_representation(
instance)
return OrderedDict([(key, result[key]) for key in result if
result[key] is not None])
Answer the question
In order to leave comments, you need to log in
I will not talk about a specific example now, but in general
the same function, but with a slight difference
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question