Thursday, April 28, 2011

Re: form CharField null vs blank

On Apr 29, 7:25 am, Michael Radziej <m...@spieleck.de> wrote:
> On Thu, 28 Apr 2011 06:18:08 -0700, Brian Craft <bc...@thecraftstudio.com> wrote:
> > A form with a CharField seems to end up with a zero-length string in
> > the cleaned_data if the field is not in the form data. Is there any
> > good way to avoid this?
>
> Django docs say:
>
> "Note that empty string values will always get stored as empty strings,
> not as NULL. Only use null=True for non-string fields such as integers,
> booleans and dates. For both types of fields, you will also need to set
> blank=True if you wish to permit empty values in forms, as the null
> parameter only affects database storage (see blank).
>
> 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. [...]

This is of course technically incorrect :-)

NULL means that you don't have any information, the empty string means
that you _know_ that the value is a string of length zero. This is
basic information theory.

Django is in good company though, most databases have problems round-
tripping one or more of the values NULL, "" (empty string), and " "
single space. What Django is doing is abstracting away inconsistent
database behavior -- and that's a good thing.

-- bjorn

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

No comments:

Post a Comment