Django-ORM之自关联表结构+处理平行数据

2022-10-0309:51:53后端程序开发Comments737 views字数 1545阅读模式

什么是自关联表结构

表内自关联是指表内数据相关联的对象和表是相同字段,这样我们就直接用表内关联将外键关联设置成自身表的字段。同样表内关联也分一对多字段和多对多字段。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28181.html

例如:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28181.html

对于微博评论,每条评论都可能有子评论,但每条评的字段内容应该都是相同的,并且每条评论都只有一个父评论,这就满足了,一对多的情形。父评论为关联字段,可以对应多个子评论,这就是一对多的自关联。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28181.html

再比方说,我们国家的省市县信息的存储上面,就是一个很典型的自关联表。一个省对应很多个市。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28181.html

在Django中创建自关联表(一对多)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28181.html

models.py:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28181.html

#评论表
class Comment(models.Model):
#评论的内容字段
content=models.CharField(max_length=255)
#评论的发布时间
push_time=models.DateTimeField(auto_now_add=True)
#关联父评论的id,可以为空
pcomment = models.ForeignKey(to='self',null=True)
def __str__(self):
return self.content文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28181.html

#省市县
class Area(models.Model):
name = models.CharField(max_length=20,verbose_name="名称")
parent = models.ForeignKey("self",verbose_name="上级行政区划")文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28181.html

class Meta:
db_table = "db"
verbose_name = "行政区划"文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28181.html

在Django中如何查询

#第一条数据的id是1,可以通过筛选父评论关联字段等于1的对象文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28181.html

Comment.objects.filter(pcomment_id=1)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28181.html

#根据子评论关联id正向查找父评论的内容文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28181.html

Comment.objects.filter(pcomment_id=2).values('pcomment__content')文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28181.html

#根据父评论的id反向查找子评论文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28181.html

Comment.objects.filter(id=1).values('comment__id')文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28181.html

"========================================================================"文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28181.html

#如果知道一个市交a市,想查它属于什么省文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28181.html

a = Area.objects.get(id=1)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28181.html

#b就是a市的身份对象文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28181.html

b = a.parent文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28181.html

#如果知道一个省,叫a省,相查他有什么市文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28181.html

#b就是a省的全部市的对象文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28181.html

b = a.area_set.all() #类名小写+“set”文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28181.html

在Django中将平行数据处理成树型方便使用

#datas为接收到的数据库经序列化后的数据文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28181.html

def xTree(datas):
lists = []
tree = {}
parent_id = ''
for i in datas:
item = i
tree[item['id']] = item
root = None
for i in datas:
obj = i
if not obj['cid']:
root = tree[obj['id']]
lists.append(root)
else:
parent_id = obj['cid']
if 'children' not in tree[parent_id]:
tree[parent_id]['children'] = []
tree[parent_id]['children'].append(tree[obj['id']])
return lists文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28181.html

版权声明:本文为CSDN博主「Rliner」的原创文章。
原文链接:https://blog.csdn.net/weixin_47073925/article/details/106504767文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/28181.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/28181.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定