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="状态") 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)
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)
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(可执行对象)
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)
二、on_delete外键删除方式
- cascade:级联删除。当manufacturer对象删除时,它对应的car对象也会删除。
- protect:保护模式,采用该选项,删除时会抛出protectederror错误。
- set_null:置空模式,删除的时候,外键字段被设置为空,前提就是blank=true, null=true,定义该字段的时候,允许为空。当manufacturer对象删除时,它对应的car对象的manufacturer字段会置空,前提是null=true
- set_default:置默认值,删除的时候,外键字段设置为默认值,所以定义外键的时候注意加上一个默认值。
- 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使用内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
推荐阅读
-
JavaScript中Promise的使用详解
-
react中Suspense的使用详解
-
Javascript中的getUTCDay()方法使用详解
-
JavaScript中的getTime()方法使用详解
-
JavaScript中setMonth()方法的使用详解
-
JavaScript中的getTimezoneOffset()方法使用详解
-
Javascript中的getUTCHours()方法使用详解
-
JavaScript中getUTCMinutes()方法的使用详解
-
JavaScript中的toDateString()方法使用详解
-
JavaScript中setUTCMilliseconds()方法的使用详解