-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Returning an object in Serializer.to_internal_value is not working any more since 3.8.2 #6053
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
Comments
Bump, |
Typically, instance creation is handled in the django-rest-framework/rest_framework/serializers.py Lines 468 to 471 in 90ed2c1
Usage would typically look like: my_serializer.is_valid(raise_exception=True)
instance = my_serializer.save() What are you expecting to do instead? |
What if there is no saving and you want to return your own type in validated_data |
If you don't want to use the builtin save() method, you can always write your own instead. e.g., class PointSerializer:
def save(self):
self.instance = Point(self.validated_data)
return self.instance
s = PointSerializer(...)
s.is_valid(raise_exception=True)
instance = s.save() |
To me it seemed like This ruins composablity since save is just a non-recursive stub. This used to work prior to 3.8.2 and i have lot of code doing similar things. class TwoPoints(serializer.Serializer):
a = PointSerializer()
b = PointSerializer()
s = TwoPoints(...)
s.is_valid(True)
assertDictEqual(s.validated_data, {'a': Point(...), 'b': Point(...) }) |
Good point. That is a discrepancy between the |
I've added it to the milestone to help ensure this is addressed in some capacity. The temporary "fix" here may just be to override |
Is there any workaround exists? I really need to update to 3.9 version and can't do it because of the problem. In our product most of serializers and fields return DTO (implemented with dataclasses) from to_internal_value. |
@mofr def run_validators(self, value):
super(Serializer, self).run_validators(value) |
Fixes encode#6053. Original test case thanks to Vincent Delaitre in encode#6242.
Fixes encode#6053. Original test case thanks to Vincent Delaitre in encode#6242.
Fixes encode#6053. Original test case thanks to Vincent Delaitre in encode#6242.
Uh oh!
There was an error while loading. Please reload this page.
Returning an object in
Serializer.to_internal_value
is not working any more since 3.8.2 (because of #5922). I don't know if it was ever intended to be working (since obviously there are no tests for it) but it did in 3.8.1. NowSerializer.to_internal_value
always has to return adict
. Otherwise a type error is raised.Checklist
master
branch of Django REST framework.Steps to reproduce
Expected behavior
Everything is working like it did in 3.8.1.
Actual behavior
TypeError: 'float' object is not iterable
raised indjango-rest-framework/rest_framework/serializers.py
Line 465 in d9f5418
The text was updated successfully, but these errors were encountered: