Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7k
Add a Writeable SerializerMethodField to DRF#8216
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
You know how the front end guys always want the format output in a weird way right? including a field where we can format the outputs will be great, there are solutions online like this: classWriteableSerializerMethodField(serializers.Field):def__init__(self,write_serializer=None,method_name=None,**kwargs):assertwrite_serializerisnotNoneandisinstance(write_serializer,serializers.Field),'write_serializer must be a valid serializer field'self.write_serializer=write_serializerself.method_name=method_namesuper(WriteableSerializerMethodField,self).__init__(**kwargs)defbind(self,field_name,parent):# In order to enforce a consistent style, we error if a redundant# 'method_name' argument has been used. For example:# my_field = serializer.CharField(source='my_field')default_method_name='get_{field_name}'.format(field_name=field_name)assertself.method_name!=default_method_name, ("It is redundant to specify `%s` on SerializerMethodField '%s' in ""serializer '%s', because it is the same as the default method name. ""Remove the `method_name` argument."% (self.method_name,field_name,parent.__class__.__name__) )# The method name should default to `get_{field_name}`.ifself.method_nameisNone:self.method_name=default_method_namesuper(WriteableSerializerMethodField,self).bind(field_name,parent)defto_internal_value(self,data):returnself.write_serializer.to_internal_value(data)defto_representation(self,value):method=getattr(self.parent,self.method_name)returnmethod(value) just wondering if it can be added to DRF. |
BetaWas this translation helpful?Give feedback.
All reactions
Replies: 3 comments 3 replies
-
can you share the output format/formats please to understand the reasoning behind it? |
BetaWas this translation helpful?Give feedback.
All reactions
-
Let's assume we have a phone field
If we have the field above we can use something like this:
|
BetaWas this translation helpful?Give feedback.
All reactions
-
i think you can do this in model level as well or them using a model method and make the output in API as well |
BetaWas this translation helpful?Give feedback.
All reactions
-
There are multiple ways to solve this yes, we can in the view for example replace the data["phone"] |
BetaWas this translation helpful?Give feedback.
All reactions
-
This is a great idea. My desired usage for such a fromdjango.contrib.auth.modelsimportUserfromdjango.utils.timezoneimportnowfromrest_frameworkimportserializersclassUserSerializer(serializers.ModelSerializer):days_since_joined=serializers.ReadWriteSerializerMethodField()classMeta:model=Userfields='__all__'defget_days_since_joined(self,obj):return (now()-obj.date_joined).daysdefset_days_since_joined(self,instance:User,value:int):instance.date_joined=now()-time_in_days(value) In the setter I can directly interact with instance of my object. |
BetaWas this translation helpful?Give feedback.
All reactions
-
While the The OP seems to imply the need for Thesecond example with a getter and setter methods might be a better API, but I can see that becoming very messy as the number of fields grow. Overall, I'm -1 on this 👎🏻 |
BetaWas this translation helpful?Give feedback.