Opened 16 months ago

Closed 16 months ago

Last modified 16 months ago

#22374 closed Bug (wontfix)

Suggest to validate unique before running clean in models full_clean etc..

Reported by: dspjmt@… Owned by: nobody
Component: Database layer (models, ORM) Version: 1.6
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

If we use clean(), and check for fields the hasn't been assigned, then it will raise a doesnotexist exception.

The clean() method claims we can utilize fields which data is clean, it's not, will in validation of uniqueness, the missing of fields will be suggested successfully, I think we should check if the field exists first.

Change History (5)

comment:1 Changed 16 months ago by timo

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Severity changed from Release blocker to Normal

Sorry, I'm having trouble understanding your report. Could you give an example?

comment:2 Changed 16 months ago by timo

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

comment:3 follow-up: Changed 16 months ago by Jimmy

Let's say, I have a module like:

class A(Model):

b = IntegerField(max_length=3)

clean(self):

if b < 3:

raise ValidationError('Too large')

When we use a form, if b is not filled, error will happen because b don't have a value. So we have to check if b has value, which was done in validate_unique(). So why not validate_unique first and then call clean?

comment:4 in reply to: ↑ 3 Changed 16 months ago by Jimmy

Replying to Jimmy:

Let's say, I have a module like:

class A(Model):

b = IntegerField(max_length=3)

Sorry, b = IntegerField()

clean(self):

if b < 3:

raise ValidationError('Too large')

When we use a form, if b is not filled, error will happen because b don't have a value. So we have to check if b has value, which was done in validate_unique(). So why not validate_unique first and then call clean?

comment:5 Changed 16 months ago by timo

  • Resolution changed from needsinfo to wontfix

I don't think it's a good idea. In particular, there is no guarantee that validate_unique() will be called. Note this comment in django.forms.models:

# self._validate_unique will be set to True by BaseModelForm.clean().
# It is False by default so overriding self.clean() and failing to call
# super will stop validate_unique from being called.
Note: See TracTickets for help on using tickets.
Back to Top