List Info

Thread: manager.get_or_create() issue




manager.get_or_create() issue
user name
2007-01-30 16:45:47
We have a model with a DateTimeField, and two FloatField's.  These three fields are unique_together in the database. 
 
Why when using get_or_create(datetimefield=someDatetimeObject, floatfield1=float(-34.2412), floatfield2=float(1.2432)) does it not return a record in the database that has those values, and then fails with a duplicate key error on the unique key of those fields?
 
 

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users"; group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to django-users-unsubscribegooglegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
Re: manager.get_or_create() issue
user name
2007-01-30 21:20:40
On Jan 30, 2007, at 2:45 PM, Curtis W. Ruck wrote:
We have a model with a DateTimeField, and two FloatField's.  These three fields are unique_together in the database. 
 
Why when using get_or_create(datetimefield=someDatetimeObject, floatfield1=float(-34.2412), floatfield2=float(1.2432)) does it not return a record in the database that has those values, and then fails with a duplicate key error on the unique key of those fields?



Float fields are difficult to represent precisely, some form of internal rounding may occur. If you need a precise value as a unique key, you could store them as strings or integers (remove the decimal point by multiplying by 10,000). Of course, you'll then have to convert values when you query the database.

Here's more on how Python handles floating point:

Don




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

about | contact  Other archives ( Real Estate discussion Medical topics )