Opened 7 years ago

Closed 5 years ago

#8890 closed (invalid)

Inline model formsets break validation for unique_together with foreign keys

Reported by: masont@… Owned by: nobody
Component: Forms Version: 1.0
Severity: Keywords:
Cc: bronger@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


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,
    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.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:

Change History (5)

comment:1 Changed 7 years ago by anonymous

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #8882.

comment:2 Changed 5 years ago by bronger

  • Resolution duplicate deleted
  • Status changed from closed to reopened

I run into the same problem with SVN 13265. The unique_constraing of the child model is not checked by the form set returned by inlineformset_factory. Instead, the integrity error is raised. My code is equivalent to the code above except that I don't have custom form classes.

comment:3 Changed 5 years ago by bronger

  • Cc bronger@… added

comment:4 Changed 5 years ago by bronger

Sorry, I meant "The unique_together" constrain of the child model ...

comment:5 Changed 5 years ago by russellm

  • Resolution set to invalid
  • Status changed from reopened to closed

Closing invalid. This report doesn't contain enough detail to reproduce the problem accurately -- it has foreign keys to models that don't exist, and a Manager object that isn't provided.

It doesn't describe the sequence of operations that are required to reproduce the problem, either -- it just talks about to 'validation", and refers to a "validation step before", but isn't clear about what validation step is the problem, or which one comes "before".

If anyone can provide a clear, reproducible test case, please reopen.

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