Monday, July 29, 2013

Re: Django not enforcing blank=False on a model

On Mon, 29 Jul 2013 18:14:50 -0700 (PDT)
Knight Samar <knightsamar@gmail.com> wrote:

> Hi,
>
> I have a model
>
> > from django.db import models
> >
> > class Organization(models.Model):
> > name = models.CharField(max_length=100,
> > blank = False, #mandatory
> > help_text = "Name of your Organization",
> > verbose_name = '',
> > unique = True,
> > primary_key = True,
> > )
> >
> >
> and a piece of code
>
> > from organization.models import Organization
> > o = Organization.objects.create()
> > o.save()
>
>
> which works *without raising an IntegrityError!* And the following also
> works just the same!
>
> > from organization.models import Organization
> > o = Organization()
>
> o.save()
>
>
> I am using SQLite3(SQLite 3.7.9 2011-11-01 00:52:41
> c7c6050ef060877ebe77b41d959e9df13f8c9b5e) as the database backend and it
> shows the following as the schema:
>
> CREATE TABLE "organization_organization" ( "name" varchar(100) NOT NULL
> > PRIMARY KEY );
>
>
> Even if SQLite3 was buggy, shouldn't Django itself enforce the constraint
> on ORM layer ?
>
> I have deleted .pyc files and also the .sqlite3 file before testing again.
> I am using Django 1.5.1
>
> What am I missing ? Why is Django *NOT* enforcing blank=False ?

Blank is for input fields. It means that when validating form input value cannot be blank. And by default django uses empty string to denote empty charfield, not null. (Except Oracle)

From <https://docs.djangoproject.com/en/1.5/ref/models/fields/#django.db.models.Field.blank>:
"Note that this is different than null. null is purely database-related, whereas blank is validation-related. If a field has blank=True, form validation will allow entry of an empty value. If a field has blank=False, the field will be required."

Also, you should note that .save() doesn't imply running validation <https://docs.djangoproject.com/en/1.5/ref/models/instances/#validating-objects>

--

Jani Tiainen

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment