最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Django數(shù)據(jù)模型中on_delete使用詳解

 更新時間:2020年11月30日 15:14:14   作者:湯圓兒2019  
這篇文章主要介紹了Django數(shù)據(jù)模型中on_delete使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

on_delete屬性針對外鍵ForeignKey

一、django3.0官方文檔介紹:

Many-to-one relationships多對一關(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.

常見的使用方式(設(shè)置為null)

class ApiList(models.Model):
 desc = models.CharField(max_length=255, verbose_name="接口描述")
 keyword = models.CharField(max_length=100, verbose_name="請求關(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)建時間")
 update_at = models.CharField(max_length=20, verbose_name="更新時間")

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)

一對一(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)和一對一(OneToOneField)的參數(shù)中可以看出,都有on_delete參數(shù),而 django 升級到2.0之后,表與表之間關(guān)聯(lián)的時候,必須要寫on_delete參數(shù),否則會報異常:

TypeError: __init__() missing 1 required positional argument: 'on_delete'

因此,整理一下on_delete參數(shù)的各個值的含義:

on_delete=None,        # 刪除關(guān)聯(lián)表中的數(shù)據(jù)時,當(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ā)錯誤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è)置為可空,一對一同理)
# models.ForeignKey('關(guān)聯(lián)表', on_delete=models.SET_DEFAULT, default='默認值')
on_delete=models.SET_DEFAULT, # 刪除關(guān)聯(lián)數(shù)據(jù),與之關(guān)聯(lián)的值設(shè)置為默認值(前提FK字段需要設(shè)置默認值,一對一同理)
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í)行對象的返回值,設(shè)置:models.SET(可執(zhí)行對象)

多對多(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)

因為多對多(ManyToManyField)沒有 on_delete 參數(shù),所以略過不提. 

二、on_delete外鍵刪除方式

  1. CASCADE:級聯(lián)刪除。當(dāng)Manufacturer對象刪除時,它對應(yīng)的Car對象也會刪除。
  2. PROTECT:保護模式,采用該選項,刪除時會拋出ProtectedError錯誤。
  3. SET_NULL:置空模式,刪除的時候,外鍵字段被設(shè)置為空,前提就是blank=True, null=True,定義該字段的時候,允許為空。當(dāng)Manufacturer對象刪除時,它對應(yīng)的Car對象的manufacturer字段會置空,前提是null=True
  4. SET_DEFAULT:置默認值,刪除的時候,外鍵字段設(shè)置為默認值,所以定義外鍵的時候注意加上一個默認值。
  5. SET():自定義一個值,該值當(dāng)然只能是對應(yīng)的實體了

django3.0關(guān)于models官方文檔地址:
1.https://docs.djangoproject.com/en/3.0/topics/db/models/
2.https://docs.djangoproject.com/en/3.0/ref/models/fields/#django.db.models.ForeignKey

到此這篇關(guān)于Django數(shù)據(jù)模型中on_delete使用詳解的文章就介紹到這了,更多相關(guān)Django on_delete使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python使用paramiko模塊通過ssh2協(xié)議對交換機進行配置的方法

    python使用paramiko模塊通過ssh2協(xié)議對交換機進行配置的方法

    今天小編就為大家分享一篇python使用paramiko模塊通過ssh2協(xié)議對交換機進行配置的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • python字符串替換第一個字符串的方法

    python字符串替換第一個字符串的方法

    這篇文章主要介紹了python字符串替換第一個字符串的方法,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-06-06
  • 詳解Python中import模塊導(dǎo)入的實現(xiàn)原理

    詳解Python中import模塊導(dǎo)入的實現(xiàn)原理

    這篇文章主要給大家介紹了Python中import模塊導(dǎo)入的實現(xiàn)原理,主要從什么是模塊,import搜索路徑以及導(dǎo)入原理這三個方面給大家介紹,感興趣的小伙伴跟著小編一起來看看吧
    2023-08-08
  • python中執(zhí)行smtplib失敗的處理方法

    python中執(zhí)行smtplib失敗的處理方法

    在本篇文章里小編給大家整理了關(guān)于python中執(zhí)行smtplib失敗的處理方法,對此有需要的朋友們可以學(xué)習(xí)下。
    2020-07-07
  • Python網(wǎng)絡(luò)爬蟲之獲取網(wǎng)絡(luò)數(shù)據(jù)

    Python網(wǎng)絡(luò)爬蟲之獲取網(wǎng)絡(luò)數(shù)據(jù)

    本文介紹了Python中用于獲取網(wǎng)絡(luò)數(shù)據(jù)的重要工具之一——Requests庫,詳細講解了Requests庫的基本使用方法、請求方法、請求頭、請求參數(shù)、Cookies、Session等內(nèi)容,并結(jié)合實例代碼展示了Requests庫的應(yīng)用場景
    2023-04-04
  • pygame 精靈的行走及二段跳的實現(xiàn)方法(必看篇)

    pygame 精靈的行走及二段跳的實現(xiàn)方法(必看篇)

    下面小編就為大家?guī)硪黄猵ygame 精靈的行走及二段跳的實現(xiàn)方法(必看篇)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • python基本算法之實現(xiàn)歸并排序(Merge sort)

    python基本算法之實現(xiàn)歸并排序(Merge sort)

    這篇文章主要給大家介紹了關(guān)于python基本算法之實現(xiàn)歸并排序(Merge sort)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • 基于Pydantic封裝的通用模型在API請求驗證中的應(yīng)用詳解

    基于Pydantic封裝的通用模型在API請求驗證中的應(yīng)用詳解

    這篇文章主要介紹了基于Pydantic封裝的通用模型在API請求驗證中的應(yīng)用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步早日升職加薪
    2023-05-05
  • 詳解使用python3.7配置開發(fā)釘釘群自定義機器人(2020年新版攻略)

    詳解使用python3.7配置開發(fā)釘釘群自定義機器人(2020年新版攻略)

    這篇文章主要介紹了詳解使用python3.7配置開發(fā)釘釘群自定義機器人(2020年新版攻略),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • Python?statistics模塊示例詳解

    Python?statistics模塊示例詳解

    這篇文章主要介紹了Python?statistics模塊示例詳解,本文總結(jié)了 statistics 模塊的常規(guī)操作,對于數(shù)據(jù)分析還是非常有益處的,需要的朋友可以參考下
    2023-05-05

最新評論

阿勒泰市| 马尔康县| 江西省| 平度市| 西畴县| 吴忠市| 庆阳市| 镇康县| 垦利县| 师宗县| 济南市| 额敏县| 沅陵县| 福海县| 延长县| 平潭县| 高州市| 中西区| 房产| 南漳县| 平乐县| 纳雍县| 仲巴县| 宁强县| 高台县| 屏东县| 木里| 石狮市| 临邑县| 方山县| 石屏县| 渑池县| 唐海县| 柯坪县| 东宁县| 岑溪市| 图木舒克市| 上饶县| 江阴市| 大渡口区| 辽宁省|