我必须用一对多的关系建模,我试图用它来区分我的记录类型.假设第一个模型专用于 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 中查询集中记录的多行相关的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!