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
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
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
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
# 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)
# 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)
null=Trueis related to the database
blank=Trueis related to form (Django Forms) verification
null=False,blank=FalseThe default configuration, which means that the value is required under any circumstances
null=True,blank=True, which means that the field is optional in all cases.
null=False,blank=True, indicating that the field does not require a value in the form, but the database requires
null=True, blank=False: indicates that the form requires a value, but the database does not.
NULLis allowed, you will end up indicating missing values in two different ways.
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 (
This may be the least commonly used configuration, but there are some use cases:
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
on_delete=models.SET_NULLto implement a simple soft delete.