Concatenation of a field related to multiple rows of a record in query set in Django(连接与 Django 中查询集中记录的多行相关的字段)
问题描述
我必须用一对多的关系建模,我试图用它来区分我的记录类型.假设第一个模型专用于 Book 信息,第二个模型是一些类型,例如 A、B、C,并且 Type 表与 Book 之间存在间接关系,因此每本书可以是 A、B 或 C 或任何可能的组合类型.我想使用串联(或注释中的任何其他可能的函数来收集字段中的所有类型).
I have to models with one to many relation with which I try to distinguish the type of my records. Let's say First model is dedicated to Book information and second model is some types such as A, B , C and there is an indirect relation from the Type table to Book, so each book could be A, B or C or any possible combination of Types. I want to use concatenation (or any other possible function in annotation to gather all the types in a field).
Book.objects.all(
).annotate(
Types = F('TableRelation__Type__Name')
).annotate(
CombinedTypes = Concat('Types')
)
抛出错误,因为只有一个参数被传递给连接.我正在寻找的结果是一个针对 Book 的任何唯一 id 填充了ABAB"的 CombinedTypes 字段,这表明该记录是AB"(或 A、B 和 C 的任何其他组合).
which throws an error since only one argument is passed to be Concatenated. The result I am looking for is a CombinedTypes field filled with "ABAB" for any unique id of Book which shows that that record is an "AB" (or any other combination of A,B and C).
我怎样才能做到这一点?
How can I achieve this?
推荐答案
我最终使用了 this 中解释的解决方案@Ahmad 在评论中介绍的帖子中的堆栈溢出答案.
I ended up using the solution explained in this Stack-overflow answer in the post introduced by @Ahmad in comments.
这篇关于连接与 Django 中查询集中记录的多行相关的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:连接与 Django 中查询集中记录的多行相关的字段


基础教程推荐
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- 带更新的 sqlite CTE 2022-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- 带有WHERE子句的LAG()函数 2022-01-01