on_delete方法如何在Django中使用?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
一、django3.0官方文檔介紹:
Many-to-one relationships多對(duì)一關(guān)系
To define a many-to-one relationship, use django.db.models.ForeignKey. You use it just like any other Field type: by including it as a class attribute of your model.
ForeignKey requires a positional argument: the class to which the model is related.
For example, if a Car model has a Manufacturer – that is, a Manufacturer makes multiple cars but each Car only has one Manufacturer – use the following definitions:
from django.db import models class Manufacturer(models.Model): # ... pass class Car(models.Model): manufacturer = models.ForeignKey(Manufacturer, on_delete=models.CASCADE) # ...
You can also create recursive relationships (an object with a many-to-one relationship to itself) and relationships to models not yet defined; see the model field reference for details.
It's suggested, but not required, that the name of a ForeignKey field (manufacturer in the example above) be the name of the model, lowercase. You can, of course, call the field whatever you want.
常見(jiàn)的使用方式(設(shè)置為null)
class ApiList(models.Model): desc = models.CharField(max_length=255, verbose_name="接口描述") keyword = models.CharField(max_length=100, verbose_name="請(qǐng)求關(guān)鍵字") response = models.TextField(verbose_name="響應(yīng)結(jié)果") api = models.ForeignKey(Api, blank=True, null=True, on_delete=models.SET_NULL, verbose_name="所屬接口") status = models.IntegerField(default=1, verbose_name="狀態(tài)") create_at = models.CharField(max_length=20, verbose_name="創(chuàng)建時(shí)間") update_at = models.CharField(max_length=20, verbose_name="更新時(shí)間")
一對(duì)多(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)
一對(duì)一(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)
從上面外鍵(ForeignKey)和一對(duì)一(OneToOneField)的參數(shù)中可以看出,都有on_delete參數(shù),而 django 升級(jí)到2.0之后,表與表之間關(guān)聯(lián)的時(shí)候,必須要寫(xiě)on_delete參數(shù),否則會(huì)報(bào)異常:
TypeError: __init__() missing 1 required positional argument: 'on_delete'
因此,整理一下on_delete參數(shù)的各個(gè)值的含義:
on_delete=None, # 刪除關(guān)聯(lián)表中的數(shù)據(jù)時(shí),當(dāng)前表與其關(guān)聯(lián)的field的行為 on_delete=models.CASCADE, # 刪除關(guān)聯(lián)數(shù)據(jù),與之關(guān)聯(lián)也刪除 on_delete=models.DO_NOTHING, # 刪除關(guān)聯(lián)數(shù)據(jù),什么也不做 on_delete=models.PROTECT, # 刪除關(guān)聯(lián)數(shù)據(jù),引發(fā)錯(cuò)誤ProtectedError # models.ForeignKey('關(guān)聯(lián)表', on_delete=models.SET_NULL, blank=True, null=True) on_delete=models.SET_NULL, # 刪除關(guān)聯(lián)數(shù)據(jù),與之關(guān)聯(lián)的值設(shè)置為null(前提FK字段需要設(shè)置為可空,一對(duì)一同理) # models.ForeignKey('關(guān)聯(lián)表', on_delete=models.SET_DEFAULT, default='默認(rèn)值') on_delete=models.SET_DEFAULT, # 刪除關(guān)聯(lián)數(shù)據(jù),與之關(guān)聯(lián)的值設(shè)置為默認(rèn)值(前提FK字段需要設(shè)置默認(rèn)值,一對(duì)一同理) on_delete=models.SET, # 刪除關(guān)聯(lián)數(shù)據(jù), a. 與之關(guān)聯(lián)的值設(shè)置為指定值,設(shè)置:models.SET(值) b. 與之關(guān)聯(lián)的值設(shè)置為可執(zhí)行對(duì)象的返回值,設(shè)置:models.SET(可執(zhí)行對(duì)象)
多對(duì)多(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)
因?yàn)槎鄬?duì)多(ManyToManyField)沒(méi)有 on_delete 參數(shù),所以略過(guò)不提.
二、on_delete外鍵刪除方式
CASCADE:級(jí)聯(lián)刪除。當(dāng)Manufacturer對(duì)象刪除時(shí),它對(duì)應(yīng)的Car對(duì)象也會(huì)刪除。
PROTECT:保護(hù)模式,采用該選項(xiàng),刪除時(shí)會(huì)拋出ProtectedError錯(cuò)誤。
SET_NULL:置空模式,刪除的時(shí)候,外鍵字段被設(shè)置為空,前提就是blank=True, null=True,定義該字段的時(shí)候,允許為空。當(dāng)Manufacturer對(duì)象刪除時(shí),它對(duì)應(yīng)的Car對(duì)象的manufacturer字段會(huì)置空,前提是null=True
SET_DEFAULT:置默認(rèn)值,刪除的時(shí)候,外鍵字段設(shè)置為默認(rèn)值,所以定義外鍵的時(shí)候注意加上一個(gè)默認(rèn)值。
SET():自定義一個(gè)值,該值當(dāng)然只能是對(duì)應(yīng)的實(shí)體了
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,的支持。
文章名稱(chēng):on_delete方法如何在Django中使用-創(chuàng)新互聯(lián)
標(biāo)題鏈接:http://jinyejixie.com/article26/dijocg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、企業(yè)建站、建站公司、移動(dòng)網(wǎng)站建設(shè)、微信小程序、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容