时间:2021-05-22
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 modelsclass Manufacturer(models.Model): # ... passclass 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)
一对多(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)一对一(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外键删除方式
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使用内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
在Django2.0以上的版本中,创建外键和一对一关系必须定义on_delete参数,我们可以在其源码中看到相关信息classForeignKey(Foreig
一、django中数据模型关于时间字段的认识1、DateField:可以记录年月日,映射到数据库是date类型2、DateTimeField:可以记录年月日时分
本篇经验将介绍如何在PowerDesigner中创建概念数据模型。概念数据模型也称信息模型,它以实体-联系(Entity-RelationShip,简称E-R)
django中设计数据模型类是基于ORM的对象关系映射更方便的进行数据库中的数据操作。对象关系映射把面向对象中的类和数据库表--对应,通过操作类和对象,对数表实
星型模型是在关系数据库设计中被广泛应用的实体——关系数据模型。在关系数据模型中,实体集合及其联系构成了数据库模式,在联机事务处理中较为适