I store Decimal numbers in JSONField with Django in order to have flexible amount of digits but then I'm unable to use them. This is how my model looks like.
from decimal import Decimal
# from django.core.serializers.json import DjangoJSONEncoder
import simplejson
class Order(models.Model):
account = models.ForeignKey(Account, on_delete=models.CASCADE, null=True)
size = JSONField(null=True)
Following the recommendation from this answer https://stackoverflow.com/a/3148376/3423825 and store the value like this:
import simplejson as json
self.size = json.dumps(Decimal('1'), use_decimal=True)
Now when I try to convert it to decimal I get an error.
size = Decimal(self.size)
Object of type 'Decimal' is not JSON serializable
I must say I'm a bit lost because type(self.size)
returns <class string>
so I should be able to convert it to Decimal
, or am'I missing something?
Thank you