Add a Writeable SerializerMethodField to DRF #8216
Replies: 3 comments 3 replies
-
can you share the output format/formats please to understand the reasoning behind it? |
Beta Was this translation helpful? Give feedback.
-
This is a great idea. My desired usage for such a from django.contrib.auth.models import User
from django.utils.timezone import now
from rest_framework import serializers
class UserSerializer(serializers.ModelSerializer):
days_since_joined = serializers.ReadWriteSerializerMethodField()
class Meta:
model = User
fields = '__all__'
def get_days_since_joined(self, obj):
return (now() - obj.date_joined).days
def set_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. |
Beta Was this translation helpful? Give feedback.
-
While the The OP seems to imply the need for The second 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 👎🏻 |
Beta Was this translation helpful? Give feedback.
-
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:
just wondering if it can be added to DRF.
Beta Was this translation helpful? Give feedback.
All reactions