[django] add new field on serializer which is not defined on underlying model instance [SOLVED]

To add a field which will be computed on Serializer itself without flowing through underlying model instance, declare it as follows:

class DbconfigExtendedSerializer(serializers.HyperlinkedModelSerializer):
password_length = serializers.SerializerMethodField('getPasswordLength')

def getPasswordLength(self, instance):
return len(instance.password)

Simply declare your field of type serializers.SerializerMethodField(‘<method_name>’) and corresponding method will be called.

sources

Post a Comment

Your email is never published nor shared. You're allow to say what you want...