Opened 16 years ago

Closed 14 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: no UI/UX: no

Description

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/

Change History (5)

comment:1 by anonymous, 16 years ago

Resolution: duplicate
Status: newclosed

Duplicate of #8882.

comment:2 by Torsten Bronger, 14 years ago

Resolution: duplicate
Status: closedreopened

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 by Torsten Bronger, 14 years ago

Cc: bronger@… added

comment:4 by Torsten Bronger, 14 years ago

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

comment:5 by Russell Keith-Magee, 14 years ago

Resolution: invalid
Status: reopenedclosed

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