model validation

0 views
Skip to first unread message

Greg Donald

unread,
Mar 10, 2007, 12:53:28 PM3/10/07
to django...@googlegroups.com
Why doesn't this prevent me from entering an empty string into the database?

name = models.CharField(maxlength=32, blank=False, null=False)


Thanks,


--
Greg Donald
http://destiney.com/

Malcolm Tredinnick

unread,
Mar 10, 2007, 2:47:40 PM3/10/07
to django...@googlegroups.com
On Sat, 2007-03-10 at 11:53 -0600, Greg Donald wrote:
> Why doesn't this prevent me from entering an empty string into the database?
>
> name = models.CharField(maxlength=32, blank=False, null=False)

I assume you mean via some way other than a form or the admin interface,
because in those two cases, field validation should catch it. At the
database level, the only thing that applies is null=False, which means
the column must contain a non-NULL value. An empty string is non-NULL,
so it satisfies the column constraint.

Malcolm


Greg Donald

unread,
Mar 10, 2007, 5:59:26 PM3/10/07
to django...@googlegroups.com
On 3/10/07, Malcolm Tredinnick <mal...@pointy-stick.com> wrote:
> I assume you mean via some way other than a form or the admin interface,

Model validation doesn't occur in the model itself?

> because in those two cases, field validation should catch it.

What is "field validation" exactly?

Malcolm Tredinnick

unread,
Mar 10, 2007, 6:07:02 PM3/10/07
to django...@googlegroups.com
On Sat, 2007-03-10 at 16:59 -0600, Greg Donald wrote:
> On 3/10/07, Malcolm Tredinnick <mal...@pointy-stick.com> wrote:
> > I assume you mean via some way other than a form or the admin interface,
>
> Model validation doesn't occur in the model itself?

Validation happens on the strings submitted from a form, *before* they
are converted to Python objects (since there's no guarantee we can
convert them without error). We do not have model-aware validation at
the moment, but it is coming. Note that what we do have is still good
for the majority of cases, because you have access to all the submitted
fields. What you don't have access to is the instance of the model that
will be affected in the case where you are updating an existing
instance. That is the feature we will add eventually.

>
> > because in those two cases, field validation should catch it.
>
> What is "field validation" exactly?

See http://www.djangoproject.com/documentation/forms/#validators for a
description of how validators work. Although that page talks about
validators in the context of form submissions, they are the same
validators that you can specify on models.*Field classes as well.

Suffice it to say that, in your case, when you submit a string as part
of a form submission to go into your field, Django will ensure that it
is non-empty, because you have blank=False.

Regards,
Malcolm


Greg Donald

unread,
Mar 11, 2007, 11:50:15 AM3/11/07
to django...@googlegroups.com
On 3/10/07, Malcolm Tredinnick <mal...@pointy-stick.com> wrote:
> Validation happens on the strings submitted from a form, *before* they
> are converted to Python objects (since there's no guarantee we can
> convert them without error). We do not have model-aware validation at
> the moment, but it is coming. Note that what we do have is still good
> for the majority of cases, because you have access to all the submitted
> fields. What you don't have access to is the instance of the model that
> will be affected in the case where you are updating an existing
> instance. That is the feature we will add eventually.

Having to validate the data at every possible form location doesn't
seem very DRY to me. I should be able to put a single set of
validation rules directly in the model so any form I write in the
future has to validate against them.

> See http://www.djangoproject.com/documentation/forms/#validators for a
> description of how validators work. Although that page talks about
> validators in the context of form submissions, they are the same
> validators that you can specify on models.*Field classes as well.

The newforms page doesn't have much about validation on it:

http://www.djangoproject.com/documentation/newforms/

Do the same validation techniques apply from the old forms system to
the newforms?

> Suffice it to say that, in your case, when you submit a string as part
> of a form submission to go into your field, Django will ensure that it
> is non-empty, because you have blank=False.

That doesn't seem to be the case for this particular field I have:

class List(models.Model):


name = models.CharField(maxlength=32, blank=False, null=False)

> python manage.py shell
In [1]: import datetime
In [2]: from mysite.groceries.models import List
In [3]: l = List(name='', created_at=datetime.datetime.now())
In [4]: l.save()
In [5]: l.name
Out[5]: ''

What am I missing here?

Reply all
Reply to author
Forward
0 new messages