Django多层嵌套ManyToMany字段ORM操作详解
作者:宅神kin 时间:2023-07-01 02:26:28
在用django写项目时,遇到了许多场景,关于ORM操作获取数据的,但是不好描述出来,百度搜索关键词都不知道该怎么搜,导致一个人鼓捣了好久。这里细化下问题,还原场景,记录踩下的坑
首先先列举model,我举些生活中的例子,更方便理解问题
# 习题
class Problem(models.Model):
desc = models.CharField()
answer = models.TextField()
is_pass = models.BooleanField(default=False, verbose_name="是否通过")
# 章节
class Chapter(models.Model):
_id = models.IntegerField(verbose_name="编号")
title = models.CharField()
problem = models.ManyToManyField(Problem)
pass_rate = models.IntegerField(verbose_name="通关率")
# 书籍
class Book(models.Model):
title = models.CharField()
desc = models.TextField()
chapter = models.ManyToManyField(Chapter,verbose_name="章节")
speed = models.IntegerField(verbose_name="学习进度", default=0)
假设是一本数学书,有5个章节,每个章节里有数量不等的习题,
即book与chapter是多对多,chapter与problem也是多对多
场景一: 书籍下的所有习题
# 按我的理解是取问题非空的章节数
# 类似于问爷爷有几个孙子,没办法跨辈,就按一个孙子对应一个爸爸来取(有重复)
book.chapter.filter(problem___id__isnull=False).count()
场景二:书籍下所有通过的习题
book.chapter.filter(problem__is_pass=True).count()
场景三: 判断某个问题是否在这本书里
def problem_in_ladder(book, problem):
for i in book.chapter.all():
if problem in i.problem.all():
return True
return False
尽可能的减少view中对models的取值操作,所以把上面几个场景方法写在models类中
最终的models
# 习题
class Problem(models.Model):
desc = models.CharField()
answer = models.TextField()
is_pass = models.BooleanField(default=False, verbose_name="是否通过")
# 章节
class Chapter(models.Model):
_id = models.IntegerField(verbose_name="编号")
title = models.CharField()
problem = models.ManyToManyField(Problem)
pass_rate = models.IntegerField(verbose_name="通关率")
@property
def items(self):
return self.problem.count()
@property
def pass_problem(self):
return self.problem.filter(is_pass=True).count()
# 书籍
class Book(models.Model):
title = models.CharField()
desc = models.TextField()
chapter = models.ManyToManyField(Chapter,verbose_name="章节")
speed = models.IntegerField(verbose_name="学习进度", default=0)
@property
def chapters(self):
return self.chapter.count()
@property
def pass_count(self):
return self.chapter.filter(problem__is_pass=True).count()
@property
def items(self):
return self.chapter.filter(problem___id__isnull=False).count()
补充知识:django中当model设置了ordering后,使用distinct()和annotate()问题记录
model类如下,我在class Meta中设置了ordering = ['-date_create'],即模型对象返回的记录结果集是按照这个字段排序的。
class SystemUserPushHistory(models.Model):
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
host_name = models.CharField(max_length=128, null=False)
system_username = models.CharField(max_length=128, null=False)
method = models.CharField(max_length=32, null=False)
is_success = models.BooleanField(default=False)
date_create = models.DateTimeField(auto_now_add=True, editable=False)
message = models.CharField(max_length=4096, null=True)
class Meta:
db_table = "assets_systemuser_push_history"
ordering = ['-date_create']
def __str__(self):
ret = self.system_username + " => " + self.host_name
return ret
当业务有需求如对host_name进行分组显示,在代码中用到了annotate,如下。
>>> from django.db.models import Count
>>> from assets.models import SystemUserPushHistory
>>> p = SystemUserPushHistory.objects.values("host_name").annotate(dcount=Count(1))
>>> p
<QuerySet [{'host_name': '点2', 'dcount': 1}, {'host_name': '点3', 'dcount': 2}, {'host_name': '点2', 'dcount': 1}, {'host_name': '点3', 'dcount': 1}]>
>>> print(p.query)
SELECT `assets_systemuser_push_history`.`host_name`, COUNT(1) AS `dcount` FROM `assets_systemuser_push_history` GROUP BY `assets_systemuser_push_history`.`host_name`, `assets_systemuser_push_history`.`date_create` ORDER BY `assets_systemuser_push_history`.`date_create` DESC
可以看到,所得到的结果并不像我们预期的一样,之后把执行的sql输出出来可以看到在group by的时候是对host_name和date_create进行分组,原因就是因为我们在model类中设置了ordering,去掉之后代码运行正常。
使用distinct和上面的情况类似,就不列出来了。
来源:https://blog.csdn.net/weixin_42042680/article/details/84845741