#23874 closed Uncategorized (worksforme)
Admin Interface: Unique constraint in gis.db.models raises Integrity Error instead of the unique error message
Reported by: | George Tantiras | Owned by: | nobody |
---|---|---|---|
Component: | GIS | Version: | 1.7 |
Severity: | Normal | Keywords: | IntegrityError, Unique Constraint, Admin |
Cc: | George Tantiras | Triage Stage: | Unreviewed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
models.py
from django.contrib.gis.db import models as geomodels 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.
Change History (5)
comment:1 by , 10 years ago
comment:3 by , 10 years ago
Cc: | added |
---|
comment:4 by , 10 years ago
Resolution: | → worksforme |
---|---|
Status: | new → closed |
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.)
comment:5 by , 10 years ago
Thank you for the very useful 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.
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?