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
"TypeError: <amount> <currency> is not JSON serializable" when serializing a calculated model fields in DRF #291
Comments
Hello @JadSayegh ! class AccountSerializer(serializers.ModelSerializer):
net_deposits = MoneyField(max_digits=14, decimal_places=2, read_only=True)
class Meta:
model = Account
fields = ('deposits', 'withdrawals', 'net_deposits') And it will process the money value as usual: >>> instance = Account.objects.create(deposits=Money(1, 'EUR'), withdrawals=Money(2, 'EUR'))
>>> serializer = AccountSerializer(instance=instance)
>>> serializer.data
{'deposits': '1.00', 'net_deposits': '-1.00', 'withdrawals': '2.00'} I hope this will help :) If you have any questions - I'd like to hear from you :) Best regards |
Thanks for the quick reply Dmitry (@Stranger6667), my
|
Since there is no such field on the model you cant use it as an automatic field. But you can do something like this: class AccountSerializer(serializers.ModelSerializer):
net_deposits = MoneyField(max_digits=14, decimal_places=2, read_only=True)
net_deposits_currency = serializers.CharField(source='net_deposits.currency')
class Meta:
model = Account
fields = ('deposits', 'withdrawals', 'net_deposits', 'net_deposits_currency') And the currency should appear in the serialized data: >>> instance = Account.objects.create(deposits=Money(1, 'EUR'), withdrawals=Money(2, 'EUR'))
>>> serializer = AccountSerializer(instance=instance)
>>> serializer.data
{'deposits': '1.00', 'net_deposits': '-1.00', 'withdrawals': '2.00', 'net_deposits_currency': 'EUR'} |
Awesome, that solves it! Clarification note for future readers: Out of curiosity, if I had a |
Hm, it depends on the context and your personal taste. From some point of view, the object is serialized - you have field and its value ( Another option - return dictionary from this method instead of Money, but it will be nested structure. In case if you want "flat" structure DRF implies, that you should use separate field for this. But you can unnest this structure later in the serialization process, but it is a bit hacky IMO. So, there are a plenty of options to do serialization :) Hope, I've answered your question Best Regards |
I'm closing this issue :) If you have any questions feel free to reopen this or create a new one |
I have model which has a calculated MoneyField:
My serializer looks like this
Whenever I try to query a model through this serializer I get the error mentioned in the title, for example:
TypeError: 80 USD is not JSON serializable
This is not an issue when the field is directly on the model (not a calculated field).
I've also tried making it a
SerializerMethodField
and returnnet_deposit
from the function and am getting the same thing. What's the correct way to get this to work?The text was updated successfully, but these errors were encountered: