The difference between django null=True and blank=True

created at 07-16-2021 views: 1

Introduction

It is important to understand that the options in the Django model field definition have at least two functions: define the database table, define the default format, and validate the model format. (The reason I say "default values" is because these values can always be overridden by providing a custom form.) Some options affect the database, some options affect the form, and some affect both forms.

null=True is equivalent to setting the column in the database to NUll.

blank=True is whether to confirm the need for this field in the form. This includes modifying the dictionary and custom forms in the management background. If blank=True, it means that the field is not needed in the form. If the field is needed in the form, then blank=False.

In practical applications, these two combinations are very frequent. In many environments, you need to allow the fields of the form to be empty, and you also need the database to store the value of the field as NULL. But CharField and TextField are an exception. In django, the user will not save as null, and the null value is stored in the database as an empty string "".

Example 1

# If it is empty, raise IntegrityError
models.DateTimeField(blank=True)

# It is not allowed to be empty, a value must be passed
models.DateTimeField(null=True) 

Example 2

# If the input is empty, then an empty string ``instead of
models.CharField(blank=True) 


# NULL is allowed, but never set to NULL
models.CharField(null=True) 

summary

  1. null=True is related to the database
  2. blank=True is related to form (Django Forms) verification
  3. null=False,blank=False The default configuration, which means that the value is required under any circumstances
  4. null=True,blank=True, which means that the field is optional in all cases.
  5. null=False,blank=True, indicating that the field does not require a value in the form, but the database requires
  6. null=True, blank=False: indicates that the form requires a value, but the database does not.

use cases of null=False,blank=True

  1. The most common use is for optional fields based on strings. As stated in the documentation, the Django idiom is to use an empty string to indicate missing values. If NULL is allowed, you will end up indicating missing values in two different ways.
  2. Another common situation is that you want to automatically calculate a field based on the value of another field (for example, using your save() method). You don't want users to provide values in some form (hence blank=True), but you want the database to force values to always be provided (null=False).
  3. Another use is when you want to indicate that a ManyToManyField is optional. Because this field is implemented as a separate table instead of a database column, null has no meaning. However, the value of blank will still affect the form, and control whether the verification is successful when it doesn’t matter.

use cases of null=True, blank=False

This may be the least commonly used configuration, but there are some use cases:

  1. It is perfectly reasonable to require users to always include a value, even if your business logic does not actually need it. After all, forms are just a way to add and edit data. The data generated by the code you may have does not require the same rigorous verification as a human editor.
  2. Another use case I have seen is when you have a ForeignKey object that you don't want to cascade to delete. In other words, in normal use, the relationship should always exist (blank=False), but if the object it points to happens to be deleted, you don't want to delete the object either. In this case, you can use null=True and on_delete=models.SET_NULL to implement a simple soft delete.
Please log in to leave a comment.