django2.2 model classes compatible


Model class introduced when the need to add foreign key

on_delete=models.CASCADE

Example:
the previous version:

name = models.ForeignKey('Name')
type = models.ForeignKey('Type',verbose_name='类别')

To be replaced by:

name = models.ForeignKey('Name',on_delete=models.CASCADE)
type = models.ForeignKey('Type',on_delete=models.CASCADE,verbose_name='类别')

 

Otherwise, error:
TypeError: the __init __ (). 1 Missing required Positional argument: 'on_delete'

 


on_delete effect: if the foreign key is associated with a corresponding data table is deleted ., then coping model class data

before django2.X version, on_delete (default = 'CASCADE' ), CASCADE is the default value, need to be updated after manual settings.




general election still CASCADE.

on_delete there are six kinds of options, in addition to CASCADE, there PROTECT, sET (), SET_NULL, sET_DEFAULT, dO_NOTHING


Detailed on_delete property:

CASCADE (cascade delete): When deleting data associated with the table, the foreign key field also will delete

PROTECT (Protected Mode): If this option is deleted when an error is thrown ProtectedError

the SET ( ): custom a value corresponding to the entity

SET_NULL (blanking): when the data in the association table to delete the foreign key blank, of course, in addition to the key field have allowed to be null, null = True

set_default (set default): deleted when the foreign key field set to the default values, so the definition of the foreign key when the attention plus a default value

do_nothing : what is not dry

Guess you like

Origin www.cnblogs.com/jrri/p/11605015.html