This answered my question.
_coordinates, coordinates_created = Coordinates.objects.get_or_create(
lat=Decimal(group[u'lat'])._rescale(-Coordinates.max_decimal_places, 'ROUND_HALF_EVEN'),
lon=Decimal(group[u'lon'])._rescale(-Coordinates.max_decimal_places, 'ROUND_HALF_EVEN'),
created_by=self._user,
modified_by=self._user,
)
The auto_now
and auto_now_add
were just fine. It turns out that all of my defaults are already defined on the Model.
The problem was that group[u'lat']
and group[u'lon']
were both cast as floats when I placed them in my dictionary. In contrast, lat
and lon
are both defined as DecimalFields()
.
When using MySQL I can compare these float values against the contents of the database just fine. However, when I use Postgres the get()
portion of get_or_create()
attempts to compare a Decimal value from the database with the the float values I was providing it. The types are interpreted more strongly, and the float will not be cast to a Decimal during the comparison.
In my debugger, I saw:
{Decimal}lat
{float}group[lat]
It would be nice if django could produce a clear error like TypeError: Can't compare Decimal with float.