I have a field
owner = models.ForeignKey(User, null=True, on_delete=models.SET_NULL)
What is the difference between these two attributes on model field ?
null is purely database-related, whereas blank is validation-related. 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.
The fundamental difference between these two is that null controls the validation at the database level, and blank is used during form field validation at the application level. By default all fields are required. In order to make a field optional, we have to say so explicitly.
simply put, on_delete is an instruction to specify what modifications will be made to the object in case the foreign object is deleted: CASCADE: will remove the child object when the foreign object is deleted. SET_NULL: will set the child object foreign key to null.
The on_delete handle is used to handle the deletion of reference data to maintain the database integrity.
null=True
means owner field can be null
in the database which means you can make an object of your model that has no owner.
on_delete=models.SET_NULL
means if the owner of an existing object got deleted set this field for existing object to null.
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