Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between null=True and on_delete=models.SET_NULL django

Tags:

python

django

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 ?

like image 265
jahmed31 Avatar asked Jun 27 '18 11:06

jahmed31


People also ask

What is difference between null and blank in Django models?

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.

What is the difference between null true and blank true in Django?

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.

What is On_delete models Set_null?

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.

What does On_delete do in Django?

The on_delete handle is used to handle the deletion of reference data to maintain the database integrity.


1 Answers

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.

like image 90
Milad Khodabandehloo Avatar answered Oct 20 '22 01:10

Milad Khodabandehloo