Editing in the admin makes None into ''

5 views
Skip to first unread message

Ram Rachum

unread,
May 20, 2012, 3:50:50 PM5/20/12
to pyweb-il
Hey all,

I just stumbled into an annoying behavior of Django that I don't know how to handle.

I discovered that when you edit a model in the Django admin, and one of its text field's value is `None`, it'll be saved as the empty string u'' instead of `None`.

This makes sense from the HTML implementation point-of-view, because the form is a text field, and if it has nothing in it, that usually means u''. But it's really annoying from the Django point-of-view, because now I have some objects with a value of u'' and some with `None`! This is causing bugs in some of my queries.

Any idea what to do about this?


Thanks,
Ram.

Emanuel Ilyayev

unread,
May 20, 2012, 6:12:30 PM5/20/12
to pywe...@googlegroups.com
The docs says:

Avoid using null on string-based fields such as CharField and TextField unless you have an excellent reason. If a string-based field has null=True, that means it has two possible values for “no data”: NULL, and the empty string. In most cases, it’s redundant to have two possible values for “no data;” Django convention is to use the empty string, not NULL.




--
You received this message because you are subscribed to the Google Groups "PyWeb-IL" group.
To post to this group, send email to pywe...@googlegroups.com.
To unsubscribe from this group, send email to pyweb-il+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pyweb-il?hl=en.

Ram Rachum

unread,
May 20, 2012, 7:54:32 PM5/20/12
to pywe...@googlegroups.com
Thanks Emanuel!

I think this is a bit of an ugly solution, since None seems to me like a more perfect representation of "Nothing" than the empty string. But since this is the Django convention, I'll go with it.


Thanks!
Reply all
Reply to author
Forward
0 new messages