When you save, you ultimately hit the error:
{{{
ValueError: PostGIS geography does not support the "~=" function/operator.
}}}
This is caused by the `validate_unique()` method on the model instance.
When performing unique checks (in `_perform_unique_checks()`), it's
forming a queryset with `objects.filter(geometry=geometry)` and (as noted
in related issue https://code.djangoproject.com/ticket/27314) that results
in the ~= operator being used in PostGIS which is not supported on
geographic geometries.
We will work around this issue ourselves, by overriding
`validate_unique()` on the model, passing the field in question to the
exclude list in the super call, then doing our own manual check by instead
using a filter like:
{{{
GeoModel.objects.filter(geometry__bboverlaps=point)
}}}
i.e., a bounding-box overlaps check.
For point fields, the concept of even having a bounding box, and how much
sense that makes, I suppose is up for debate (points theoretically being
of infinitely small size). However this check performs in the way we want
and works as you might expect the equality operator to.
Have not confirmed whether the issue exists for geometry fields other than
Point, or on versions other than 3.2 - but I strongly suspect so.
Simplest test case would be to have a models.py:
{{{
from django.contrib.gis.db import models
class Location(models.Model):
geometry = models.PointField(geography=True, unique=True)
}}}
along with admin.py
{{{
from django.contrib.gis import admin
admin.site.register(Location, admin.OSMGeoAdmin)
}}}
and then try and save an existing instance when using the postgis db
backend.
--
Ticket URL: <https://code.djangoproject.com/ticket/33638>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.