断关联
1、外键位置:
一对多 - 外键放多的一方
一对一 - 从逻辑正反向考虑,如作者表与作者详情表,作者删除级联删除详情,详情删除作者依旧存在,所以建议外键在详情表中
多对多 - 外键在关系表中
2.关联表之间断关系(全部写在外键字段的括号里)
db_constraint=False, # 断关联
related_name=‘books‘, # 反向查询字段:publish_obj.books 就能访问所有出版的书(名字可随意取)
2、ORM正向方向连表查找:
正向:通过外键字段 eg: author_detial_obj.author
反向:通过related_name的值 eg:author_obj.detail
注:依赖代码见下方
3、连表操作级联关系(以作者表和详情表(外键在此表)一对一为例,写在外键字段括号里):
1)级联删除:(作者删除,详情也删除);(详情删除,作者不删除) - on_delete=models.CASCADE
2)作者删除,详情表外键字段值置空其他字段值不变 - null=True, on_delete=models.SET_NULL
3)作者删除,详情表外键字段值重置为默认值 - default=0, on_delete=models.SET_DEFAULT
4)作者删除,详情表对应那一整条信息不动 - on_delete=models.DO_NOTHING
注:拿作者与作者详情表举例
4、外键关联字段的参数 - 如何实现 断关联、目前表间操作关系、方向查询字段
i)作者详情表中的
author = models.OneToOneField(
to=‘Author‘,
related_name=‘detail‘,
db_constraint=False,
on_delete=models.CASCADE
)
ii)图书表中的
publish = models.ForeignKey(
to=‘Publish‘,
related_name=‘books‘,
db_constraint=False,
on_delete=models.DO_NOTHING,
)
authors = models.ManyToManyField(
to=‘Author‘
related_name=‘books‘,
db_constraint=False,
)
注:ManyToManyField不能设置on_delete,OneToOneField、ForeignKey必须设置on_delete(django1.x系统默认级联,但是django2.x必须手动明确)
model类
from django.db import models
# 图书管理系统:Book、Author、AuthorDetail、Publish
"""
Book表: name、price、img、authors、publish、is_delete、create_time
Publish表: name、address、is_delete、create_time
Author表: name、age、is_delete、create_time
AuthorDetail表: mobile, author、is_delete、create_time
"""
# 1) 基表
class BaseModel(models.Model):
is_delete = models.BooleanField(default=False)
create_time = models.DateTimeField(auto_now_add=True)
# 作为基表的Model不能在数据库中形成对应的表,设置 abstract = True
class Meta:
abstract = True
class Book(BaseModel):
"""name、price、img、authors、publish、is_delete、create_time"""
name = models.CharField(max_length=64)
price = models.DecimalField(max_digits=5, decimal_places=2)
img = models.ImageField(upload_to=‘img‘, default=‘img/default.jpg‘)
publish = models.ForeignKey(
to=‘Publish‘,
db_constraint=False, # 断关联
related_name=‘books‘, # 反向查询字段:publish_obj.books 就能访问所有出版的书
on_delete=models.DO_NOTHING, # 设置连表操作关系
)
authors = models.ManyToManyField(
to=‘Author‘,
db_constraint=False,
related_name=‘books‘
)
# 序列化插拔式属性 - 完成自定义字段名完成连表查询
@property
def publish_name(self):
return self.publish.name
@property
def author_list(self):
return self.authors.values(‘name‘, ‘age‘, ‘detail__mobile‘).all()
class Meta:
db_table = ‘book‘
verbose_name = ‘书籍‘
verbose_name_plural = verbose_name
def __str__(self):
return self.name
class Publish(BaseModel):
"""name、address、is_delete、create_time"""
name = models.CharField(max_length=64)
address = models.CharField(max_length=64)
class Meta:
db_table = ‘publish‘
verbose_name = ‘出版社‘
verbose_name_plural = verbose_name
def __str__(self):
return self.name
class Author(BaseModel):
"""name、age、is_delete、create_time"""
name = models.CharField(max_length=64)
age = models.IntegerField()
class Meta:
db_table = ‘author‘
verbose_name = ‘作者‘
verbose_name_plural = verbose_name
def __str__(self):
return self.name
class AuthorDetail(BaseModel):
"""mobile, author、is_delete、create_time"""
mobile = models.CharField(max_length=11)
author = models.OneToOneField(
to=‘Author‘,
db_constraint=False,
related_name=‘detail‘,
on_delete=models.CASCADE,
)
class Meta:
db_table = ‘author_detail‘
verbose_name = ‘作者详情‘
verbose_name_plural = verbose_name
def __str__(self):
return ‘%s的详情‘ % self.author.name
modleserializers序列化
from rest_framework.serializers import ModelSerializer, SerializerMethodField
from rest_framework.exceptions import ValidationError
from . import models
# 可以单独作为Publish接口的序列化类,也可以作为Book序列化外键publish辅助的序列化组件
class PublishModelSerializer(ModelSerializer):
class Meta:
model = models.Publish
fields = (‘name‘, ‘address‘)
class BookModelSerializer(ModelSerializer):
# 了解: 该方式设置的序列化字段,必须在fields中声明
# publish_address = SerializerMethodField()
# def get_publish_address(self, obj):
# return obj.publish.address
# 自定义连表深度 - 子序列化方式 - 该方式不能参与反序列化,使用在序列化反序列化共存时,不能书写
publish = PublishModelSerializer()
class Meta:
# 序列化类关联的model类
model = models.Book
# 参与序列化的字段
fields = (‘name‘, ‘price‘, ‘img‘, ‘author_list‘, ‘publish‘)
# 了解知识点
# 所有字段
# fields = ‘__all__‘
# 与fields不共存,exclude排除哪些字段
# exclude = (‘id‘, ‘is_delete‘, ‘create_time‘)
# 自动连表深度
# depth = 1
数据库表关系--断关联