-
Notifications
You must be signed in to change notification settings - Fork 159
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
Error when accessing serializer.data that contains price-related fields (For example, CheckoutSerializer) #188
Comments
It indeed looks like the OrderTotalCalculator is returning a Price object and not a My first thought is that the current CheckoutSerializer total field definition is wrong and should be replaced by a PriceSerializer But I need more investigation for this, thanks for reporting! |
…rice Use a different field for the calculated order total. See also #188
Use a different field for the calculated order total. See also #188
Use a different field for the calculated order total. See also #188
Use a different field for the calculated order total. See also #188
As already thought, the input type for the BTW, accessing the data attribute is a bit funky here as the See also the comment by Tom Christie. Access |
@mhoonjeon The pull request is merged, see the comments above. Thanks you your input, closing this issue now |
@maerteijn Sorry I checked this feedback a little late; I use gitlab for work most of the times and I only check github from time to time :(. Anyways, thanks for the feedback and I really appreciate how much you guys have put into this open source project. Keep up the good work!! |
Use a different field for the calculated order total. See also django-oscar/django-oscar-api#188
I overrode the checkoutserializer(added an extra
sender_info
field), so I was writing some tests on the serializer itself.The raised error was
decimal.InvalidOperation: [<class 'decimal.ConversionSyntax'>]
.I error message was not clear at first, so I did some digging in, and I found that the error was raised within the serializer's
to_representation
block.It seems the
attribute
is Price class return by the following line, in oscarapi's checkoutserializer's validate method.total = OrderTotalCalculator().calculate(basket, shipping_charge)
So the attribute looks like this:
Price(currency='USD', excl_tax=Decimal('10000.00'), incl_tax=Decimal('10000.00'), tax=Decimal('0.00'))
So in order to access the serializer.data, one might have to handle decimal fields(
total
in this checkoutserializer) individually by using attribute.incl_tax as a parameter toto_representation
method, when dealing with price-related fields. Or you can probably handle this at field level too.This error may be trivial because it does
NOT
affect the core logic and purpose of the CheckoutSerializer - so this may not have to be fixed.But I'm leaving this issue as a reference for someone who might need to access
serializer.data
like me. :) (FYI, you can still accessserializer.validated_data
without any extra error handling)The text was updated successfully, but these errors were encountered: