What is the meaning of the required on_delete parameter in the django2.0 association table
This article mainly introduces what is the meaning of the required on_delete parameters in the django2.0 correlation table. It is very detailed and has a certain reference value. Interested friends must read it!
One to many (ForeignKey)
Class ForeignKey (ForeignObject): def _ _ init__ (self, to, on_delete, related_name=None, related_query_name=None, limit_choices_to=None, parent_link=False, to_field=None, db_constraint=True, * * kwargs): super (). _ init__ (to, on_delete, from_fields= ['self'], to_fields= [to _ field], * * kwargs)
One to one (OneToOneField)
Class OneToOneField (ForeignKey): def _ init__ (self, to, on_delete, to_field=None, * * kwargs): kwargs ['unique'] = True super (). _ _ init__ (to, on_delete, to_field=to_field, * * kwargs)
As can be seen from the above foreign key (ForeignKey) and one-to-one (OneToOneField) parameters, there is an on_delete parameter. After django is upgraded to 2.0, the on_delete parameter must be written when the table is associated with the table, otherwise an exception will be reported:
TypeError: _ _ init__ () missing 1 required positional argument: 'on_delete'
So, sort out the meaning of the values of the on_delete parameter:
On_delete=None, # when deleting the data in the associated table, the behavior of the field associated with the current table on_delete=models.CASCADE, # deleting the associated data, also deleting the associated on_delete=models.DO_NOTHING, # deleting the associated data, doing nothing on_delete=models.PROTECT, # deleting the associated data, causing an error ProtectedError# models.ForeignKey ('Associated Table', on_delete=models.SET_NULL, blank=True) Null=True) on_delete=models.SET_NULL, # delete the associated data, set the associated value to null (if the FK field needs to be set to nullable, one-to-one same) # models.ForeignKey ('associated table', on_delete=models.SET_DEFAULT, default=' default') on_delete=models.SET_DEFAULT, # delete the associated data, and set the associated value to the default value (provided that the FK field needs to set the default value, one-to-one same) on_delete=models.SET # delete associated data, a. The value associated with it is set to the specified value, setting: models.SET (value) b. The value associated with it is set to the return value of the executable object, setting: models.SET (executable object)
Many to many (ManyToManyField)
Class ManyToManyField (RelatedField): def _ _ init__ (self, to, related_name=None, related_query_name=None, limit_choices_to=None, symmetrical=None, through=None, through_fields=None, db_constraint=True, db_table=None, swappable=True, * * kwargs): super (). _ init__ (* * kwargs)
Because many-to-many (ManyToManyField) does not have an on_delete parameter, it is skipped.
The above is all the content of the article "what is the meaning of the required on_delete parameter in the django2.0 correlation table". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!