Django inserts an empty string into IntegerField?

1,637 views
Skip to first unread message

Huuuze

unread,
Jun 25, 2008, 4:03:02 PM6/25/08
to Django users
I have defined a zip code field as follows in my Address model:

zip = models.IntegerField(blank=True, null=True)

Being a good Djangonaut, I'm using a ModelForm to convert my address
model into HTML. Now this where things get dicey. As you can see in
the model, zip code can except empty values. Unfortunately, when I
attempt to save data entered by a user who has left the zip code field
blank, I receive a database exception. Apparently, Django is
attempting to insert an empty string into my integer-only field.

From what I can tell, this particular topic has been discussed
previously:

http://www.hoboes.com/Mimsy/?ART=595

Is the solution outlined in the blog post above the long-term solution
for Django developers or are there plans to have a ModelForm
understand that I would not want to insert an empty string into an
integer field?

Norman Harman

unread,
Jun 25, 2008, 4:28:36 PM6/25/08
to django...@googlegroups.com

Are you sure it happens in IntegerFields, that's surprising. That blog
is talking about CharFields for which saving "" for None is less surprising.

Instead of mucking with model's save method I'd create my own field,
NullCharField or NullIntegerField that replaces "" or None with NULL
according to the null=true parameter.

--
Norman J. Harman Jr.
Senior Web Specialist, Austin American-Statesman
___________________________________________________________________________
You've got fun! Check out Austin360.com for all the entertainment
info you need to live it up in the big city!

Huuuze

unread,
Jun 25, 2008, 4:39:09 PM6/25/08
to Django users
Thanks for getting back to me. You're correct -- that article is not
a one-for-one match when compared to my problem, so let me tweak my
question.

When I attempt to save (my_form.save()) and I leave the zip code field
blank, I receive the following exception:

DataError at /person/new/add/
invalid input syntax for integer: ""

When I look at the insert statement, sure enough, an empty string is
attempting to make its way into my zip code field.

With that being said, do you expect that behavior? If not, how can I
correct the issue? If you do expect that behavior, how can I go about
creating a NullIntegerField? This is new territory for me.

Karen Tracey

unread,
Jun 25, 2008, 4:44:51 PM6/25/08
to django...@googlegroups.com
On Wed, Jun 25, 2008 at 4:39 PM, Huuuze <huu...@orgoo.com> wrote:

Thanks for getting back to me.  You're correct -- that article is not
a one-for-one match when compared to my problem, so let me tweak my
question.

When I attempt to save (my_form.save()) and I leave the zip code field
blank, I receive the following exception:

DataError at /person/new/add/
invalid input syntax for integer: ""

When I look at the insert statement, sure enough, an empty string is
attempting to make its way into my zip code field.

With that being said, do you expect that behavior?  If not, how can I
correct the issue?  If you do expect that behavior, how can I go about
creating a NullIntegerField?  This is new territory for me.

No, I don't expect that behavior.  If I have an IntegerField with blank=True,null=True and create a ModelForm for the associated model, then when I fill in the form and leave that field blank, Django inserts a null into the database for that field, not a blank.  So something is going wrong in your case.  What level of Django are you using?  Snippets from your code might also help in identifying what is going wrong.

Karen

Huuuze

unread,
Jun 25, 2008, 4:55:42 PM6/25/08
to Django users
You're right -- I left out one detail. To ensure the zip code field
renders in a more ideal manner, I added the following code to my
ModelForm:

>> zip = forms.CharField(max_length=5, widget=widgets.TextInput({'size':5}), required=False)

Adding "null=True" here doesn't seem to work. Any thoughts?

Norman Harman

unread,
Jun 25, 2008, 5:02:49 PM6/25/08
to django...@googlegroups.com
Huuuze wrote:
> Thanks for getting back to me. You're correct -- that article is not
> a one-for-one match when compared to my problem, so let me tweak my
> question.
>
> When I attempt to save (my_form.save()) and I leave the zip code field
> blank, I receive the following exception:
>
> DataError at /person/new/add/
> invalid input syntax for integer: ""
>
> When I look at the insert statement, sure enough, an empty string is
> attempting to make its way into my zip code field.
>
> With that being said, do you expect that behavior? If not, how can I
> correct the issue? If you do expect that behavior, how can I go about
> creating a NullIntegerField? This is new territory for me.

I don't know what ver of Django you're using by my version certainly
appears that it doesn't perform the way you say it does. i.e. it looks
like it does not allow "" for Integer fields.

from '/django/db/models/fields/__init__.py'

class IntegerField(Field):
empty_strings_allowed = False

Although, It's possible my 3min analysis of what empty_strings_allowed
does is wrong. You'd have to look closer at the above file but it looks
like overriding this

def get_db_prep_save(self, value):
"Returns field's value prepared for saving into a database."
return value

is what your after. I've not done this before though so "caveat
programmer".


btw, I really think IntegerField is the wrong column type for a zip
code. They can be upto 11 chars long and have hyphens. I'd make it
CharField or maybe even a USZipCodeField from
'contrib/localflavor/us/forms.py' There's many others in localflavor if
your flavor isn't US.

Huuuze

unread,
Jun 25, 2008, 5:12:09 PM6/25/08
to Django users
Between you and Karen, I've resolved the problem. I had a bug here:

>> zip = forms.CharField(max_length=5, widget=widgets.TextInput({'size':5}), required=False)

That code has been changed to:

>> zip = forms.IntegerField(widget=widgets.TextInput({'size':5, 'maxlength':5}), required=False)

And good call on the USZipCodeField widget. I'll be certain to make
us of that.

Side question: I have noticed that empty CharFields (i.e., an empty
middle name value) get empty string values in the database rather than
null values. This seems odd. Is that by design with Django? Here is
the code from my model for that ultimately gets rendered by my
ModelForm:

>> middle_name = models.CharField(max_length=30, blank=True, null=True)

Brian Luft

unread,
Jun 25, 2008, 6:00:28 PM6/25/08
to Django users
In response to your side question:

http://www.djangoproject.com/documentation/model-api/#null

-Brian

Collin Grady

unread,
Jun 25, 2008, 6:00:58 PM6/25/08
to Django users
Yes, an empty form field would be an empty string, not None, by
design.

Adding a null would be odd, but possible with custom clean behavior on
a form :)
Reply all
Reply to author
Forward
0 new messages