Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Django ORM allow me to omit parameters for NOT NULL fields when creating an object?

Stupid question time. I seem to be able to create an object for a Django model even though I omit a column that was defined as NOT NULL and I don't understand why. Here's my model:

class Movie(models.Model):
    name = models.CharField(max_length=256)
    length_minutes = models.IntegerField()
    rating = models.CharField(max_length=2)

    class Meta:
        db_table = 'movies'

When I run python manage.py sql I see:

BEGIN;
CREATE TABLE "movies" (
  "id" serial NOT NULL PRIMARY KEY,
  "name" varchar(256) NOT NULL,
  "length_minutes" integer NOT NULL,
  "rating" varchar(2) NOT NULL
);
COMMIT;

Also, if I run the command \d movies from the psql, I can see that all columns are designated NOT NULL.

But here's what I don't understand. When I run the Python shell, I can issue the following command and a new row with an empty 'name' column will be created:

Movie.objects.create(length_minutes=120, rating='PG')

However, if I issue (what I believe to be) the equivalent SQL command:

INSERT INTO movies(length_minutes, rating) VALUES(120, 'PG');

... I get the error I would expect: "ERROR: null value... violates not-null constraint."

Why does Django's ORM allow me to create an object that lacks a parameter for a NOT NULL CharField column? Does it assume that I'm using model validators? If so, it seems to be a pretty dumb and trusting ORM.

I'm using Python 2.7.1, Django 1.4, and PostgreSQL 9.1.4.

Thanks for your help.

like image 516
Jim Avatar asked Oct 14 '12 04:10

Jim


People also ask

How do I allow null values in Django?

null=True will make the field accept NULL values. Blank values for Django field types such as DateTimeField or ForeignKey will be stored as NULL in the database.

What is the difference between null and blank in Django models?

null is purely database-related, whereas blank is validation-related(required in form). If null=True , Django will store empty values as NULL in the database . 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.

Why we use null true in Django?

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;” the Django convention is to use the empty string, not NULL .

How does ORM work in Django?

From IDEA to Product Using Python / Django ORM stands for Object Relational Mapper. The main goal of ORM is to send data between a database and models in an application. It maps a relation between the database and a model. So, ORM maps object attributes to fields of a table.


1 Answers

After a great deal of online research and experimentation, what I've found indicates that the behavior I described above is normal Django behavior. Apparently, Django doesn't validate models by default. Furthermore, the default value for a CharField is the empty string. In order to ensure that Django raises the expected IntegrityError if I omit a CharField parameter designated NOT NULL, I needed to add "default=None" to the signature declaration:

name = models.CharField(max_length=256, default=None)

Can I get credit for answering my own question?

like image 72
Jim Avatar answered Sep 29 '22 16:09

Jim