Python - Django - ORM common field properties

Field parameters:

null: used to represent a field can be empty

unique: If set unique = True, then the field in this table must be unique

db_index: If db_index = True, it represents the database index set for this field

default: set a default value for the field

 

Relationship field parameters:

to: Set the table to be associated

to_field: Sets the table associated field

related_name: reverse operation, field names used for the table name _set 'when reverse query instead of the original

related_query_name: reverse query operation, connected prefix for substitution table name

db_constraint: whether to create a foreign key constraint in the database, the default is True

The behavior of the line when you delete data associated with the table, the current table associated with it: on_delete

on_delete values:

models.CASCADE: delete the associated data, also associated with deleted

models.DO_NOTHING: delete the associated data, causing errors IntegrityError

models.PROTECT: delete the associated data, causing errors ProtectedError

models.SET_NULL: delete the associated data, the value associated therewith is null (the precondition ForeignKey field needs to be set to empty)

models.SET_DEFAULT: delete the associated data (provided ForeignKey field to set the default value) associated with the value set to default values

models.SET: delete the associated data, the value associated with the specified value, provided: models.SET (value); value associated with the return value executable object is provided: models.SET (executable object )

Models django.db Import from 
 
 
class the Person (models.Model): 
    ID = models.AutoField (primary_key = True) 
    name = models.CharField (MAX_LENGTH = 32) 
    Age = models.IntegerField () 
    Birthday = models.DateField (auto_now_add = True ) 
 
 
class Man (models.Model): 
    the above mentioned id = models.AutoField (primary_key = True) 
    # to = "Person" represents the association with Person 
	# to_field = "name" represents the name field of the Person table with the association 
    # on_delete models.CASCADE association = when data is deleted, the association will also be deleted 
    # db_constraint = False represents two linked data not related to operations on the database 
    name = models.ForeignKey (to = "Person ", on_delete = models.CASCADE, db_constraint = False)

 

Guess you like

Origin www.cnblogs.com/sch01ar/p/11285264.html