Why CharFields don't have default=None?

22 views
Skip to first unread message

Djoume

unread,
Nov 15, 2009, 3:29:00 PM11/15/09
to Django developers
Dear Djangonauts,

I have read the documentation and I think I understand why we don't
want to store empty strings as NULL in the database.

I also understand that model validation is being worked on and it's
not what I'm trying to achieve here.

The question of why CharField default to empty strings has already
been discussed a couple of years ago on this mailing list, without any
conclusive answer:

http://groups.google.com/group/django-developers/browse_thread/thread/681a64eeb905da05/1ccd44ea84df1258?lnk=gst&q=empty+string#1ccd44ea84df1258

In this thread Malcolm Tredinnick explain that the choice of
null=False has been made because it makes more sense in the context of
a web form. This makes sense to me.

But considering this model and it's model form:

class Contact(models.Model):
name = models.CharField(max_length=255)
message = models.TextField()

def __unicode__(self):
return u'name: %s, message: %s' % (self.name, self.message)


class ContactForm(forms.ModelForm):
class Meta:
model = Contact


and the following script:

from contactus.models import Contact, ContactForm
Contact.objects.create()
<Contact: name: , message: >
ContactForm({}).errors
{'message': [u'This field is required.'], 'name': [u'This field is
required.']}


I'm making the following observations:

1. When passed empty values, only the form raises a ValidationError.
2. If I add default=None to the model both raise an exception
(ValidationError or IntegrityError).
3. If instead I set blank=True none of them raise any exception.

So my question is:

Since CharField (and childs) default to blank=False, why don't they
have default=None as well?

I believe this would be more consistent with FloatField and
IntegerField for instance, what am I missing?

--
Djoume

Russell Keith-Magee

unread,
Nov 16, 2009, 12:52:31 AM11/16/09
to django-d...@googlegroups.com
On Mon, Nov 16, 2009 at 4:29 AM, Djoume <djo...@taket.org> wrote:
> Dear Djangonauts,
>
> I have read the documentation and I think I understand why we don't
> want to store empty strings as NULL in the database.
>
> I also understand that model validation is being worked on and it's
> not what I'm trying to achieve here.
>
> The question of why CharField default to empty strings has already
> been discussed a couple of years ago on this mailing list, without any
> conclusive answer:
>
> http://groups.google.com/group/django-developers/browse_thread/thread/681a64eeb905da05/1ccd44ea84df1258?lnk=gst&q=empty+string#1ccd44ea84df1258

To my reading, that thread gives a conclusive answer.

> Since CharField (and childs) default to blank=False, why don't they
> have default=None as well?
>
> I believe this would be more consistent with FloatField and
> IntegerField for instance, what am I missing?

Malcolm explains the difference in one of his responses. It all comes
down to form processing.

When you're processing an IntegerField or FloatField on a form, you
can look at the field contents, and if the contents is an empty
string, you can interpret it as a None/NULL. If there is a value, it
needs to be cast to an integer/float.

CharFields don't have this luxury. There's no way to differentiate on
form input between "the user left this field empty and wants to store
a NULL" and "the user left this field empty and wants to store an
empty string".

Django has made a value judgement that an empty field in a form will
be interpreted as an empty string. If you don't like this default
behaviour, you can override it at the form level.

Yours
Russ Magee %-)

Zach Mathew

unread,
Nov 16, 2009, 10:23:08 AM11/16/09
to Django developers


Russell Keith-Magee <freakboy3...@gmail.com> wrote:
> Django has made a value judgement that an empty field in a form will
> be interpreted as an empty string. If you don't like this default
> behaviour, you can override it at the form level.

That makes sense for forms (where input is coming from the end user),
but why does Django have to make this judgment on models (where input
is coming from python code or has been cleaned by python code)?

I too find this behaviour odd because Django is creating a default
value for my model charfield when I have not specified a default. If I
wanted empty string to be the default I would have put default='' in
the charfield definition.

--
Zach
Reply all
Reply to author
Forward
0 new messages