与Django查询集中的记录的多行相关的字段的串联 [英] Concatenation of a field related to multiple rows of a record in query set in 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).
我该如何实现?
推荐答案
我最终使用了此中介绍的解决方案@Ahmad在评论中引入的帖子中的堆栈溢出答案.
I ended up using the solution explained in this Stack-overflow answer in the post introduced by @Ahmad in comments.
这篇关于与Django查询集中的记录的多行相关的字段的串联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!