如何计算模型对象跨对象之间的全部不构度多次关系?
假设我有以下模型:
class Tag(Model):
name = CharField()
class Book(Model):
title = CharField()
tags = ManyToManyField(Tag)
现在假设我有3本书,每本书都有3个标签。对于某些书籍来说,某些标签可能相同,没关系。
Book 1 (Tags = "tag1", "tag2", "tag3")
Book 2 (Tags = "tag1", "tag4", "tag5")
Book 3 (Tags = "tag4", "tag5", "tag6")
如何计算Django Orm查询中的所有不固有标签,从而获得9个?
Assume I have the following models:
class Tag(Model):
name = CharField()
class Book(Model):
title = CharField()
tags = ManyToManyField(Tag)
Now suppose I have 3 books, and each book has 3 tags. Some of the tags might be the same for some of the books, it doesn't matter.
Book 1 (Tags = "tag1", "tag2", "tag3")
Book 2 (Tags = "tag1", "tag4", "tag5")
Book 3 (Tags = "tag4", "tag5", "tag6")
How can I count all non-distinct tags in a Django ORM query so that I get 9 as a result?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以计数:
这将计算 all
book
s的标签数。如果您有一组book
s,则可以计数以下方式:You can count with:
This will count the number of tags for all
Book
s. If you have a set ofBook
s, you can count with: