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.
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.
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.
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 .
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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With