Add a Writeable SerializerMethodField to DRF #8216
Unanswered
HHHMHA
asked this question in
Ideas & Suggestions
Replies: 3 comments 3 replies
|
can you share the output format/formats please to understand the reasoning behind it? |
3 replies
|
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. |
0 replies
|
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 👎🏻 |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
just wondering if it can be added to DRF.
All reactions