In Django models.py, what's the difference between default, null, and blank?

Question:

  • null=True
  • blank=True
  • default = 0

What’s the difference? When do you use what?

Asked By: TIMEX

||

Answers:

Direct from Django model field reference:

Field.null

If True, Django will store empty values as NULL in the database. Default is False.

Note that empty string values will always get stored as empty strings, not as NULL. Only use null=True for non-string fields such as integers, booleans and dates. For both types of fields, you will also need to set blank=True if you wish to permit empty values in forms, as the null parameter only affects database storage (see blank).

Avoid using null on string-based fields such as CharField and TextField unless you have an excellent reason. 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;” Django convention is to use the empty string, not NULL.

Field.blank

If True, the field is allowed to be blank. Default is False.

Note that this is different than null. null is purely database-related, whereas blank is validation-related. If a field has blank=True, validation on Django’s admin site will allow entry of an empty value. If a field has blank=False, the field will be required.

Field.default

The default value for the field. This can be a value or a callable object. If callable it will be called every time a new object is created.

Answered By: Chris Morgan

From docs:

null If True, Django will store empty
values as NULL in the database.
Default is False.

blank If True, the field is allowed to
be blank. Default is False.

default The default value for the
field.

You can use “default” to set the value that will be used for the field in question should your code not explicitly set it to a value.

Use “blank” for form validation purposes – blank=True will allow the field to be set to an empty value

Use “null” if you would like to store an empty value as “null” in the DB. Often it’s preferred, however, to set blank values to an empty string or to 0 as appropriate for a given field.

Answered By: Ben Regenspan

In implementation terms:

The ‘blank’ field corresponds to all forms. Specifies if this value is required in form and corresponding form validation is done.
‘True’ allows empty values.

The ‘null’ field corresponds to DB level. It will be set as NULL or NOT NULL at the DB.

Hence if leave a field empty in admin with blank=true, NULL is fed into the DB. Now this might throw an error if that particular column in the DB is specified as NOT NULL.

Answered By: aliasav

I know you already have your answer however till this day it’s difficult to judge whether to put null=True or blank=True or both to a field. I personally think it’s pretty useless and confusing to provide so many options to developers. Let the handle the nulls or blanks however they want.

I follow this table (from the book “Two Scoops of Django”):
When to use null and blank

When to use null and blank

Answered By: saran3h

Null: It is database-related. Defines if a given database column will accept null values or not.

Blank: It is validation-related. It will be used during forms validation, when calling form.is_valid().

Default: All Time it store the given value(default value) to the field if one doesn’t provide any value for this field.

The default values of null and blank are False.

That being said, it is perfectly fine to have a field with null=True and blank=False. Meaning on the database level the field can be NULL, but in the application level it is a required field.

Now, where most developers get it wrong: Defining null=True for string-based fields such as CharField and TextField. Avoid doing that. Otherwise, you will end up having two possible values for “no data”, that is: None and an empty string. Having two possible values for “no data” is redundant. The Django convention is to use the empty string, not NULL.

Answered By: sharif_42