On Sun, 2007-10-21 at 04:47 -0700, Andreas Pfrengle wrote:
> Hello group,
> 
> I've stumbled upon sth. where I'm not sure if this is an intended
> behaviour or not. I tried to dump my db-data to a json-fixture and
> load it again, where I got the error that a float-value can't be
> converted to Decimal (using postgres with psycopg2). I traced the
> problem back and set up a Test-application with models.py, settings.py
> and a little run.py that creates an entry in my test-db. I've pasted
> all together with the fixture I get from dumpdata and the error when
> trying to load it:
> http://dpaste.com/hold/23003/
> 
> As far as I can see, the problem lies in the __init__ of the model. If
> the float-value I create here would be created directly, everything is
> fine and I get a string-conversion of the float-value in the fixture,
> which I can load without problems. But in this case, even if I
> manually put "" around the float and load it, the __init__ always
> creates a new random value. Surely I could work around this problem by
> taking the code out of __init__, but I'd like to know why django
> behaves this way; or could it even be a bug?

This is normal Python behaviour. There's nothing Django-specific going
on here.

Firstly, Python won't create decimal objects out of a float, since the
precision is not well-defined. So you can need a decimal object or a
string in order for Python to coerce it to a decimal object in
self.factor.

Secondly, look at what happens when you create a Testtable instance,
which the deserialiser is going to do by essentially doing the
equivalent of Testtable(pk=1, factor=1.2345). First, Django's default
__init__ method on the model class is run (line 8 in your paste) and
then you replace self.factor with your own random number (lines 9 and
10). It's your own code doing exactly what you told it to do.

Regards,
Malcolm


-- 
Telepath required. You know where to apply... 
http://www.pointy-stick.com/blog/


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to