Python---The use of ORM in Django (4) field, parameters (On_delete focus) Supplement

Source: Internet
Author: User

1. Index:

Normal index: Speed up search

Unique index: Speed up lookup, unique constraints

Primary KEY index: Speed up lookup, unique index, not empty

 class   UserInfo (models. Model): username  = models. Charfield ( null  =false, #不允许为空 db_column   = ' user '  , #自定义列名, you can modify the column names in the re-database  max_length  =   =true, #普通索引, can only speed up find # u Nique  =true, #约束索引, accelerated find, UNIQUE constraint # primary  =tr UE, #主键索引, accelerated lookup, UNIQUE constraint, not empty)  
    Gender = models. Booleanfield (        Default=True    #设置默认值    )

Meta-class use:

Used to generate federated indexes, and to set individual parameters

#元类信息classMeta: # table name generated in database default app name+ Underline +class name Db_table="App01_un"# Federated Index # Index_together= [        #     ("username","Gender"), #] # federated unique index Unique_together= (("username","Gender") #Integrityerror:columns user, gender is not unique # admin in the table name shown Verbose_name ="User Information"

One-to-many foreign key establishment:

    El = models. ForeignKey (        to ="emailinfo", #关联表        to_field='ID  ',  #关联列名
Db_constraint=true #是否在数据库该表中创建外键约束 (foreign key function lookup is still present, but does not have the constraint function)
Limit_choices_to=none, #在Admin或者ModelForm中显示数据时, provide limited data display
--limit_choices_to={' ID_GT ': 5}
--limit_choices_to=lambda:{' ID_GT ': 5} #或者lambda表达式 )
class EmailInfo (models. Model):    = models. Emailfield (        null=False,        db_index=true,        unique=true,    ) 
Foreign Key Association table EmailInfo

Add Information:

#添加信息 UserInfo.objects.create (username="Zhang San", Gender=True, el_id=1) UserInfo.objects.create (username="John Doe", Gender=True, el_id=2    )
# because the only constraint is to bind the name, gender together, so when not at the same time, it is possible to add UserInfo.objects.create (username = "Harry", Gender=false, el_id=3) UserInfo.objects.create (username= "Harry", Gender=true, el_id=3 ) UserInfo.objects.create (username="Zhao Liu", Gender=True, el_id=4) EmailInfo.objects.create (**{'Email':'[email protected]'}) EmailInfo.objects.create (**{'Email':'[email protected]'}) EmailInfo.objects.create (**{'Email':'[email protected]'})

For foreign keys, delete is the default cascade delete, when deleting one, the corresponding data will also be deleted, so we sometimes need to modify this nature, in creating the foreign key is to use On_delete can

For several settings for On_delete:

def set_test ():     return 4
El =models. ForeignKey ( to="EmailInfo", #关联表 To_field='ID', #关联列名NULL=True,default=2, #删除自己数据, has no effect on the associated party #on_delete= None, #当关联的email数据被删除时候, the data is also deleted and does not exist #on_delete= models. CASCADE, #性质和None一样是默认删除
#on_delete= models. Do_nothing, #当关联的数据被删除时, not affected by themselves, do nothing, do not error
#on_delete= models. PROTECT, #关联保护, because the mailbox data is associated with a user table, the mailbox is not allowed to be deleted, and triggers protecterror #Cannot Delete some instances of model'EmailInfo'Because they is referenced through aprotectedFOREIGN key:'Userinfo.el' ", <queryset [<userinfo:userinfo object>]>
#on_delete =models. Set_null, #删除关联数据, you are set to null (the prerequisite field needs to be set to nullable null=True) #对于测试models. Set_null, we need to understand, for the basic field settings of the database, like null,max_length These properties, we need to re-generate the data table, #对于on_delete等Django中的附属属性, we can dynamically modify, do not need to regenerate the data table , so if we start without setting null=True (False by default), we need to regenerate the data table #on_delete= models. Set_default, #删除关联数据后, set your own data to the default value, you need to set the Defaults property
#on_delete=models. SET (3 ) #和SET_DEFAULT相似, except that you do not need to set a default value, you can set the On_delete directly in set ()= models. Set (set_test) #和SET一样, except that the value is set to the callback function, the function needs to be set to global, callable)

Python---The use of ORM in Django (4) field, parameters (On_delete focus) Supplement

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.