Closed mbrochh closed 4 years ago
After hours of debugging I found out the following:
At this line (https://github.com/dominno/django-moderation/blob/master/src/moderation/fields.py#L48) you call the JSON deserializer.
Django will then call this line (https://github.com/django/django/blob/master/django/core/serializers/json.py#L35):
for obj in PythonDeserializer(simplejson.load(stream), **options):
It calls the PythonDeserializer. Since that deserializer expects to get a list of Python objects, Django tries to convert our JSON string via simplejson.load(stream) which, by default, will try to deserialize "0.00" into a float. If we called simplejson.load(stream, use_decimal=True), all would be good.
It is totally beyond me why the Django devs did not accept two option dicts, one for the simplejson.load call and one for the PythonDeserializer call.
So, unless there is another generic way to tell Django to use use_decimal for simplejson.load calls, I think, we must not
use obj_generator = serializers.deserialize(self.serialize_format, value.encode(settings.DEFAULT_CHARSET))
Instead we would need our own deserializer who calls
for obj in PythonDeserializer(simplejson.load(stream), **options): yield obj
but allows kwargs for the simplejson.load call.
Hope that makes sense.
Yes, that make sense. I will make a fix this. Thank you very much for finding this.
I can't reproduce this error. I tried following:
My model is:
class ModelWithDecimal(models.Model):
cost = CurrencyField()
Then i did:
obj = ModelWithDecimal.objects.create(cost='20.00')
from moderation.fields import SerializedObjectField
json_field = SerializedObjectField()
json_field._serialize(obj)
Out[0]: '[{"pk": 1, "model": "test_app1.modelwithdecimal", "fields": {"cost": "20.00"}}]'
json_field._deserialize('[{"pk": 1, "model": "test_app1.modelwithdecimal", "fields": {"cost": "20.00"}}]')
Out[0]: <ModelWithDecimal: ModelWithDecimal object>
Could you show me how do you create your object ? And please show me what is the output of json_field._serialize(your_object)
I tested this with python 2.6, django 1.2 and 1.3.
Hmpf. Weird. I will have little time this week. I will try to look into this by Sunday...
+1. Experiencing the same issue.
@caseypt Could you give more info, how your serialized object look like ? How your model look like ?
Closed because of inactivity. Please feel free to open new issue.
I'm trying to moderate the creation of a django-shop Product model. This model has a field 'unit_price', which is a CurrencyField, which is just a fancy DecimalField (see https://github.com/divio/django-shop/blob/master/shop/util/fields.py#L6)
When I create a new product, I get the following error:
Traceback: File "/home/martin/Envs/marketto/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response
Exception Type: TypeError at /shop/products/create/ Exception Value: Cannot convert float to Decimal. First convert the float to a string
I really have no clue why there is a Float involved at all... Are there known issues with DecimalFields?