class GeoUnique(geomodels.Model):
geo_point = geomodels.PointField(
unique=True,
srid=4326
)
geoobjects = geomodels.GeoManager()
}}}
In the admin interface, if the same PointField is entered twice, an
IntegrityError rises with a 40x Bad Request, while in any other of the
django.db.models if the unique constraint is "on" and "activated" a
friendly message notifies the user that the model instance cannot be
saved.
--
Ticket URL: <https://code.djangoproject.com/ticket/23874>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
Comment:
When quickly testing this, I was not able to reproduce the problem. Could
you provide us with a sample project to reproduce your issue? Which
backend are you using?
--
Ticket URL: <https://code.djangoproject.com/ticket/23874#comment:1>
Comment (by raratiru):
Thank you for the reply.
This repository [https://github.com/raratiru/geoDjangobug/tree/master]
represents the issue described with the minimum possible changes to the
default django installation.
Pay attention -however- to incorporate the changes intoduced by this fix
[https://github.com/django/django/pull/3451/files] which is not included
in Django 1.7 release.
--
Ticket URL: <https://code.djangoproject.com/ticket/23874#comment:2>
* cc: raratiru (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/23874#comment:3>
* status: new => closed
* resolution: => worksforme
Comment:
Thanks for the sample project, was really useful to debug the issue.
As you are manually building the Point object in your custom save()
method, the admin cannot detect magically that the point is a duplicate.
In your case, you could solve this by adding a `unique_together =
('longitude', 'latitude')` index to your model, or by providing a custom
`ModelForm` for your admin and creating the point in the form before the
unique validation. (Note that your model is not ideal in that it
duplicates data in the database, but that's another issue.)
--
Ticket URL: <https://code.djangoproject.com/ticket/23874#comment:4>
Comment (by raratiru):
Thank you for the very useful the solutions provided in this post. I will
try to delineate the method I use to create a PointField in the admin,
since I am actually trying to enter an address and automatically save the
coordinates.
--
Ticket URL: <https://code.djangoproject.com/ticket/23874#comment:5>