Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#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 Claude Paroz, 10 years ago

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?

comment:2 by George Tantiras, 10 years ago

Thank you for the reply.

This repository https://github.com/raratiru/geoDjangobug/tree/master represents the issue described with the minimum possible changes https://github.com/raratiru/geoDjangobug/commit/a6d367b044ba780b5f9066ed1a43d4b1b8c9a4d6 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.

Version 1, edited 10 years ago by George Tantiras (previous) (next) (diff)

comment:3 by George Tantiras, 10 years ago

Cc: George Tantiras added

comment:4 by Claude Paroz, 10 years ago

Resolution: worksforme
Status: newclosed

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 George Tantiras, 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.

Last edited 10 years ago by George Tantiras (previous) (diff)
Note: See TracTickets for help on using tickets.
Back to Top