-
Notifications
You must be signed in to change notification settings - Fork 768
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
use to_representation in favor of get_attribute #848
Conversation
to_representation will convert the datetime field into a string representation. However the to_representation on the method field will only call its underlying method.
to_represenation
in favor of get_attribute
This looks like a good improvement, maybe needs a unit test. As for the CI failure, you need to apply black formatting to the file you modified |
I appreciate that :) I thought about using the example which is provided on the rest_framework website: https://www.django-rest-framework.org/api-guide/fields/#serializermethodfield For that I have added a new Fake Model because the created field cannot be set to a fixed date.
The serializer would look like that:
At this point I have currently no clue what to do next. This is the test function in its current state:
it fails because |
Add an
You probably have this filled-in some of the date processing, passing a date for the datetime model field is a good candidate. Push the test on CI, it's easier to follow the stacktrace. |
by using date's class contructor instead of fromisostring
I have now pushed a positiv test. However I get an error for
|
Check if you have the same black==19.3b0 version as the CI, the mismatch can be producing the fail. The django=master break does not seem related with your PR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome work
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. Thanks @B4rtware
The rest_framework offers the ability to use
SerializerMethodFields
which I also wanted to use in my project together with graphene-django but this field gets the wrong return value.A detailed description can be found here: #760.
This is my copied comment from #760 which explains the change:
After further investigation, I found the origin of the underlying problem. Digging to the method
graphene_django>rest_framework>mutation.py>SerializerMutation>perform_mutate
and applying this modificationit worked.
The issue is at
field.get_attribute(ob)
. Inspecting theSerializerMethodField
inside of the rest_framework you can see that there is noget_attribute
function instead it will use theget_attribute
from the base classField
which I think (not really traced it completely) just returns the representation of theobj
. In my case this is the user object and it will return the users username because__str__
is overridden to do so.So I was interested in how the django_rest_framework handles the
get_attribute
issue. And I have found out that there is a method calledto_representation
ON theSerializer
class which indeed returns all field names and also the correct values for each field:And I am curious why the
serializer.to_representation
was not used and instead the loop over each field. Maybe I will get it after I am using this modification.Using the mentioned method above, the function could be reduced to:
Which I will now use as a workaround.
Any feedback is appreciated.