Opened 5 years ago

Closed 5 years ago

#13824 closed (duplicate)

ModelValidation ignores blank=True

Reported by: anonymous Owned by: nobody
Component: Documentation Version: 1.2
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:



I'm not quite sure if this is a feature or a bug, so sorry if i misunderstood this.

I am using a Model "Foo" that has a field like this:

class Foo(models.Model):
    uid = models.ForeignKey(UserProfile, blank=True)

Next, I'm using this Model in a Form (via ModelForm):

class FooForm(ModelForm):

    class Meta:
        model = Foo

I expect Django to ignore the uid when calling is_valid(), as it's blank attribute is set True , but I get a ValueError:

ValueError at /foo/add/

Cannot assign None: "Foo.uid" does not allow null values.

Request Method:         POST
Request URL:    http://localhost:8000/de/foo/add/
Django Version:         1.2.1
Exception Type:         ValueError
Exception Value:        

Cannot assign None: "Foo.uid" does not allow null values.

Exception Location:     <cut>/lib/python2.6/site-packages/django/db/models/fields/ in __set__, line 314

This is since at the point where is_valid() is valled, uid is set to None, since it is not sent by the form but filled in my view (it's request.user.get_profile().user_id).

I looked for this in the Django docs, but didn't get a proper solution but using full_clean() which would need me to adapt the code & use try/except .

Change History (4)

comment:1 Changed 5 years ago by kmtracey

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

You need both blank=True and null=True. The first allows blanks to be submitted as valid form values, the second allows nulls to be stored in the database field. See:

comment:2 Changed 5 years ago by R3dFox

Hm, but I do not want the field to be allowed to store NULL ?

comment:3 Changed 5 years ago by R3dFox

  • Resolution invalid deleted
  • Status changed from closed to reopened

Further, uid_id may never be NULL due to django policies.

comment:4 Changed 5 years ago by kmtracey

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

OK, if you actually want to support no entry in the form but require some value in the DB (which implies your code is somehow going to set the value to something after calling is_valid()) then this is a dupe of #13776.

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