tags:

views:

431

answers:

1

I have a django model, which has a int field (with null=True, blank=True). Now when I get a form submit from the user, I assign it like so:

my_model.width= form.cleaned_data['width']

However sometimes I get an error:

ValueError: invalid literal for int() with base 10: ''

I was wandering if it's the blank ('') string value that gets assigned to the field? Because my understanding was the model will treat blank string as null/blank?

Any help would be appreciated in this matter. Thanks.

+4  A: 

No, it doesn't. If you want to assign NULL, use Python's None. Otherwise Django will try to parse a number from the string and that fails for the empty string.

You can use the or construct to achieve this:

my_model.width = form.cleaned_data['width'] or None
Ferdinand Beyer