#25987: Creating new object on adminsite with child model inlines and
unique_together raises IntegrityError
----------------------------+--------------------
     Reporter:  MrYoda      |      Owner:  nobody
         Type:  Bug         |     Status:  new
    Component:  Forms       |    Version:
     Severity:  Normal      |   Keywords:
 Triage Stage:  Unreviewed  |  Has patch:  0
Easy pickings:  0           |      UI/UX:  0
----------------------------+--------------------
 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:
 {{{#!python
 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:
 {{{#!python
 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)

--
Ticket URL: <https://code.djangoproject.com/ticket/25987>
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 unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/049.0657cdcd0ec7e83edf0b71e9e600b223%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to