#16159: ModelForm does not catch unique error with model inheritance of more than 2 levels -------------------------------------+------------------------------------- Reporter: mturtle | Owner: nobody Type: Bug | Status: new Milestone: | Component: Database layer Version: 1.2 | (models, ORM) Keywords: unique constraint, | Severity: Normal ModelForm, inheritance | Triage Stage: Unreviewed Has patch: 0 | Easy pickings: 0 -------------------------------------+------------------------------------- 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])` -- Ticket URL: <https://code.djangoproject.com/ticket/16159> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To post to this group, send email to django-updates@googlegroups.com. To unsubscribe from this group, send email to django-updates+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-updates?hl=en.