Opened 13 years ago

Closed 13 years ago

#16159 closed Bug (duplicate)

ModelForm does not catch unique error with model inheritance of more than 2 levels

Reported by: mturtle Owned by: nobody
Component: Database layer (models, ORM) Version: 1.2
Severity: Normal Keywords: unique constraint, ModelForm, inheritance
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Ticket #12881 describes the main problem, which was partially fixed. However, when you have a larger inheritance hierarchy, the form doesn't catch it in the validation, and instead a unique constraint error is raised by postgres.

The culprit:

class A(Model):
    username = CharField(max_length=255, unique=True)

class B(A):
    pass

class C(B):
    pass

(Note C extends B, and B extends A.)

In Django 1.2.5 in django/db/models/base.py, around line 762, I see the following:

        fields_with_class = [(self.__class__, self._meta.local_fields)]
        for parent_class in self._meta.parents.keys():
            fields_with_class.append((parent_class, parent_class._meta.local_fields))

When I change self._meta.parents.keys() to include the entire chain of superclasses, the problem is fixed. I don't know the best-practice way of calculating the chain of superclasses up to but not including Model, but my dirty hack method of demonstrating this is by changing self._meta.parents.keys() to self._meta.get_base_chain(type(self).__bases__[0].__bases__[0])

Change History (1)

comment:1 by Karen Tracey, 13 years ago

Resolution: duplicate
Status: newclosed

This #15321, I believe.

Note: See TracTickets for help on using tickets.
Back to Top