Code

Opened 7 years ago

Closed 6 years ago

#4895 closed (duplicate)

newforms form_for_* methods don't automatically validate against unique_together

Reported by: Jungle Owned by: nobody
Component: Forms Version: master
Severity: Keywords: IntegrityError django.newforms
Cc: sam@…, listuser@…, larlet@…, gabor@…, micsco@…, ipse.reg@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

method is_valid don't return error for unique fields
I'm using this model

class Account(models.Model):
    provider = models.ForeignKey(Provider, verbose_name = 'Provider')
    percent = models.PositiveSmallIntegerField(u'Percent', null = True, blank = True)
    
    aid = models.PositiveIntegerField(u'Affiliate ID')
    skey = models.CharField(u'Secret key', maxlength = 256, blank = True)
    
    class Meta:
        unique_together = (('provider', 'aid'),)

When I try create instance of Account with exists fields in DB it raise IntegrityError
P.S. sorry for my English

Attachments (0)

Change History (17)

comment:1 Changed 7 years ago by anonymous

  • Cc sam@… added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 7 years ago by SmileyChris

  • Summary changed from IntegrityError django.newforms to newforms form_for_* methods don't automatically validate against unique_together
  • Triage Stage changed from Unreviewed to Design decision needed

You're right, they don't automatically validate against unique_together currently.

Whether they should or not is up for debate.

comment:3 Changed 7 years ago by mtredinnick

  • Triage Stage changed from Design decision needed to Accepted

This gets fixed once Jacob has finished the model-aware validation stuff we started working on at OSCON. Things like this must be validated in the model, not the form, but for the form_for_* cases, the model's errors should then be fed back to the form. So the problem is real, but we are fixing it in a more holistic fashion than just tweaking form_for_*.

comment:4 Changed 7 years ago by brosner

Marked #5736 as a duplicate. unique and unique_together will be fixed with some model aware validation that Malcolm mentions above.

comment:5 Changed 7 years ago by nix

  • Cc listuser@… added

comment:6 Changed 7 years ago by david

  • Cc larlet@… added

comment:7 Changed 7 years ago by nix

What is the ETA on the "model aware validation" changes? (I am trying to decide how much effort to spend working around this bug)

comment:8 Changed 7 years ago by Gábor Farkas <gabor@…>

  • Cc gabor@… added

comment:9 Changed 7 years ago by micsco

  • Cc micsco@… added

comment:10 Changed 7 years ago by ipse <ipse.reg@…>

  • Cc ipse.reg@… added

Any progress on this?
I'm still getting this exception when UNIQUE is violated.

comment:11 Changed 7 years ago by david

form_for_* are now deprecated and replaced by ModelForm so the bug should be tested with it I guess.

comment:12 Changed 7 years ago by ipse <ipse.reg@…>

It is still here with ModelForms (I'm using them, not form_for_*).

comment:13 Changed 6 years ago by Favo Yang <favo@…>

also missing field's unique=True support.

comment:14 follow-up: Changed 6 years ago by mrts

See http://www.djangosnippets.org/snippets/649/ for a workaround that can be used until model validation gets integrated.

comment:15 Changed 6 years ago by anonymous

comment:16 in reply to: ↑ 14 Changed 6 years ago by anonymous

Replying to mrts:

See http://www.djangosnippets.org/snippets/649/ for a workaround that can be used until model validation gets integrated.

doesn't handle i18n

comment:17 Changed 6 years ago by mrts

  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #6845.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.