﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
8890	Inline model formsets break validation for unique_together with foreign keys	masont@…	nobody	"I currently have the following forms:
{{{
class PrefSheetForm(ModelForm):
    DESIRED_INTS = zip(range(1, 5), range(1, 5))
    desired_dances = fields.TypedChoiceField(choices=DESIRED_INTS, coerce=int)
    class Meta:
        model = PrefSheet
        exclude = ('user','show', 'audition_number',)
    class Media:
        js = ('js/jquery.js', 'js/prefsheet.js',)

class PrefForm(ModelForm):
    class Meta:
        model = Pref

PrefFormSet = inlineformset_factory(PrefSheet, Pref, form=PrefForm, extra=2)
}}}

and the relevant models:
{{{
class PrefSheet(models.Model):
    audition_number = models.IntegerField(blank=True, null=True)
    user = models.ForeignKey(auth.models.User, related_name='prefsheets')
    conflicts = models.TextField(blank=True)
    desired_dances = models.PositiveSmallIntegerField()
    show = models.ForeignKey(core.models.Show)
    objects = PrefSheetManager()
    def __str__(self):
        return ""%s / %s"" % (self.user, self.show)
    class Meta:
        unique_together = (('user', 'show',), ('audition_number', 'show'),)
        permissions = (('can_list', 'Can list prefsheets'),)


class Pref(models.Model):
    prefsheet = models.ForeignKey(PrefSheet, related_name='prefs')
    dance = models.ForeignKey(core.models.Dance, related_name='prefs')
    pref = models.PositiveSmallIntegerField()
    def __str__(self):
        return ""%s: %s"" % (self.dance, self.pref)
    class Meta:
        ordering = ('prefsheet', 'pref')
        unique_together = (('prefsheet', 'dance'), ('prefsheet', 'pref'),)
}}}

When using PrefFormSet, violating a unique_together constraint produces an IntegrityError, despite the validation step before.  It appears that the inlineformset_factory helper removes the foreign key field from the produced forms, which prevents validate_unique from actually validating like it is supposed to.

A traceback from the error can be found here: http://dpaste.com/76055/"		closed	Forms	1.0		invalid		bronger@…	Unreviewed	0	0	0	0	0	0
