Opened 9 years ago
Closed 9 years ago
#25987 closed Bug (fixed)
Creating new object in admin with child model inlines and unique_together raises IntegrityError
Reported by: | Anton | Owned by: | Simon Charette |
---|---|---|---|
Component: | Forms | Version: | dev |
Severity: | Normal | Keywords: | admin, create object, unique_together, IntegrityError, validate_unique |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I'm getting an IntegrityError when trying to create a model (ParentModel) with inlines for ChildModel which have unique_together constraint on (ParentModel, some_field).
When ParentModel object already exists and I try to add duplicates for ChildModel, BaseModelFormSet.validate_unique() nicely prevents me to save data and prints the standard django adminsite error "Please correct the duplicate values below" with concrete message for duplicate inline rows "'Please correct the duplicate data for some_field".
models.py:
from django.db import models class ParentModel(models.Model): title = models.CharField(max_length=100, verbose_name='Title') class ChildModel(models.Model): parent = models.ForeignKey(ParentModel, verbose_name='Link to parent') some_field = models.IntegerField(verbose_name='Some numeric field for unique_together') class Meta: unique_together = (('parent', 'some_field'), )
admin.py:
from django.contrib import admin from .models import * class ChildModelFormInline(admin.TabularInline): model = ChildModel class ParentModelAdmin(admin.ModelAdmin): inlines = (ChildModelFormInline, ) class ChildModelAdmin(admin.ModelAdmin): pass admin.site.register(ParentModel, ParentModelAdmin) admin.site.register(ChildModel, ChildModelAdmin)
I think it's important issue. If our "ParentModel" has many "ChildModels" with many fields and inlines, people don't want lose their data on http 500 due to IntegriryError. Googling gives me workarounds only.
Done this on Django 1.7.11, 1.8.7 and 1.9 with SQLite (3.8.2 2013-12-06) and PostgreSQL (9.4.3)
Change History (8)
comment:1 by , 9 years ago
Keywords: | admin create object unique_together IntegrityError validate_unique added |
---|
comment:2 by , 9 years ago
comment:3 by , 9 years ago
Summary: | Creating new object on adminsite with child model inlines and unique_together raises IntegrityError → Creating new object in admin with child model inlines and unique_together raises IntegrityError |
---|---|
Triage Stage: | Unreviewed → Accepted |
From a quick look, I'm not sure if the ticket component should be "Forms" or "contrib.admin" -- can the issue be reproduced outside the admin?
comment:4 by , 9 years ago
In my humble opinion of django's middle developer, the issue can be reproduced in all cases when we use BaseModelFormSet or its descendants. In most cases it's used in admin (and without overriding of buggy method).
comment:5 by , 9 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
Version: | → master |
I just hit this issue using the admin but I can reproduce using basic inlines. I'll submit a patch with tests shortly.
comment:7 by , 9 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
I went deeper in formsets and wrote this for ChildModel:
It works fine and gracefully as I think. Workaround or not?