欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Django数据模型中on_delete使用详解

程序员文章站 2022-03-19 11:20:43
on_delete属性针对外键foreignkey一、django3.0官方文档介绍:many-to-one relationships多对一关系to define a many-to-one rel...

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="状态")
 create_at = models.charfield(max_length=20, verbose_name="创建时间")
 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,   # 删除关联数据,引发错误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. 与之关联的值设置为可执行对象的返回值,设置:models.set(可执行对象)

多对多(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.
2.https://docs.djangoproject.com/en/3.0/ref/models/fields/#django.db.models.foreignkey

到此这篇关于django数据模型中on_delete使用详解的文章就介绍到这了,更多相关django on_delete使用内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关标签: Django on delete