欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Django數據模型中on_delete使用詳解

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

on_delete屬性針對外鍵ForeignKey

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

Many-to-one relationships多對一關系

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.

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

class ApiList(models.Model):
 desc = models.CharField(max_length=255, verbose_name="接口描述")
 keyword = models.CharField(max_length=100, verbose_name="請求關鍵字")
 response = models.TextField(verbose_name="響應結果")
 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)的參數中可以看出,都有on_delete參數,而 django 升級到2.0之后,表與表之間關聯的時候,必須要寫on_delete參數,否則會報異常:

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

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

on_delete=None,        # 刪除關聯表中的數據時,當前表與其關聯的field的行為
on_delete=models.CASCADE,   # 刪除關聯數據,與之關聯也刪除
on_delete=models.DO_NOTHING, # 刪除關聯數據,什么也不做
on_delete=models.PROTECT,   # 刪除關聯數據,引發(fā)錯誤ProtectedError
# models.ForeignKey('關聯表', on_delete=models.SET_NULL, blank=True, null=True)
on_delete=models.SET_NULL,  # 刪除關聯數據,與之關聯的值設置為null(前提FK字段需要設置為可空,一對一同理)
# models.ForeignKey('關聯表', on_delete=models.SET_DEFAULT, default='默認值')
on_delete=models.SET_DEFAULT, # 刪除關聯數據,與之關聯的值設置為默認值(前提FK字段需要設置默認值,一對一同理)
on_delete=models.SET,     # 刪除關聯數據,
 a. 與之關聯的值設置為指定值,設置:models.SET(值)
 b. 與之關聯的值設置為可執(zhí)行對象的返回值,設置: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 參數,所以略過不提. 

二、on_delete外鍵刪除方式

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

django3.0關于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

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

相關文章

  • 分享幾種python 變量合并方法

    分享幾種python 變量合并方法

    這篇文章主要介紹了分享python 變量的合并幾種方法,分享內容有l(wèi)ist 合并和str 合并以及dict 合并的分析,下面具體方法介紹,需要的小伙伴可以參考一下
    2022-03-03
  • python發(fā)送多人郵件沒有展示收件人問題的解決方法

    python發(fā)送多人郵件沒有展示收件人問題的解決方法

    這篇文章主要為大家詳細介紹了python發(fā)送多人郵件沒有展示收件人問題的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-06-06
  • python讀寫csv文件的方法

    python讀寫csv文件的方法

    這篇文章主要介紹了python讀寫csv文件的方法,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值 ,需要的朋友可以參考下
    2019-08-08
  • 解決pycharm安裝第三方庫失敗的問題

    解決pycharm安裝第三方庫失敗的問題

    這篇文章主要介紹了pycharm安裝第三方庫失敗的解決方法,本文通過圖文實例相結合給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-05-05
  • Python使用turtule畫五角星的方法

    Python使用turtule畫五角星的方法

    這篇文章主要介紹了Python使用turtule畫五角星的方法,運行該程序可以看到箭頭間歇移動繪制五角星的效果,涉及Python使用turtle及time模塊繪制圖形的相關技巧,需要的朋友可以參考下
    2015-07-07
  • python中rc1什么意思

    python中rc1什么意思

    在本篇文章里小編給大家整理了關于python中rc1的基礎知識點內容,需要的朋友們可以學習下。
    2020-06-06
  • Python?中設置請求的最大重試次數示例代碼

    Python?中設置請求的最大重試次數示例代碼

    本篇文章介紹了為什么我們會收到錯誤消息,指出超出了最大重試次數,以及我們如何在?Python?中為請求設置?max_retries,本文通過示例代碼給大家介紹的非常詳細,需要的朋友參考下吧
    2023-06-06
  • Python selenium 父子、兄弟、相鄰節(jié)點定位方式詳解

    Python selenium 父子、兄弟、相鄰節(jié)點定位方式詳解

    這篇文章主要介紹了Python selenium 父子、兄弟、相鄰節(jié)點定位方式詳解的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-09-09
  • Python OpenCV使用dlib進行多目標跟蹤詳解

    Python OpenCV使用dlib進行多目標跟蹤詳解

    這篇文章主要為大家介紹了如何使用 dlib 庫在實時視頻中有效地跟蹤多個對象,文中的示例代碼講解詳細,對我們學習OpenCV有一定幫助,需要的可以參考一下
    2022-03-03
  • python+django快速實現文件上傳

    python+django快速實現文件上傳

    本篇文章主要介紹了django快速實現文件上傳,通過django web框架來實現一些簡單的功能,有需要的可以了解一下。
    2016-10-10

最新評論