SQL has a unique way of specifying an empty value, which is called NULL.
NULL represents unknown data in the database, there are 3 main meanings:
1) Know the data exists, but do not know the specific value.
2) Do not know whether the data exists.
3) data does not exist.
In SQL, the null value differs from an empty string, just as None in Python is different from an empty string (""). This means that the value of a character field (such as VARCHAR) cannot contain both null and an empty string.
This can lead to unnecessary ambiguity or confusion. Why does this record have a null, and that record has an empty string? Are there differences between them, or are data input inconsistent? Also: How can I get all the records with null values, should I find them by null and empty strings? Or just search by string?
To disambiguate, the Django generation create TABLE statement automatically adds not NULLfor each field. Here is an example of the Author module generated in the fifth chapter:
CREATE TABLE books_author " ( " ID serial not NULL PRIMARY KEY, Span style= "color: #800000;" > " first_name " varchar (30 last_name " varchar (40 " email varchar (75) not NULL)
In most cases, this default behavior is best for your application because it allows you to no longer have headaches due to data consistency. And it works well with other parts of Django. In Administrative Tools, if you leave a character field blank, it inserts an empty string for this (and * is not *NULL).
However, there are exceptions to other data types: date, time, and numeric fields do not accept empty strings. If you try to insert an empty string into a date or integer field, you may get an error returned by the database, depending on the type of the database. (PostgreSQL is strictly forbidden, will throw an exception; MySQL may or may not be accepted, depending on the version you are using and your luck.) In this case,null is the only method that specifies a null value. In the Django module, you can specify that a field is allowed to be nullby adding null=true .
so that's a little tricky to say: if you want to allow a date type (datefield , timefield , datetimefield ) or digital type (integerfield , < Span class= "Pre" >decimalfield , floatfield field is empty, you need to use null=true * and * blank=true .