問題描述
我必須用一對多的關(guān)系建模,我試圖用它來區(qū)分我的記錄類型.假設(shè)第一個模型專用于 Book 信息,第二個模型是一些類型,例如 A、B、C,并且 Type 表與 Book 之間存在間接關(guān)系,因此每本書可以是 A、B 或 C 或任何可能的組合類型.我想使用串聯(lián)(或注釋中的任何其他可能的函數(shù)來收集字段中的所有類型).
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')
)
拋出錯誤,因為只有一個參數(shù)被傳遞給連接.我正在尋找的結(jié)果是一個針對 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.
這篇關(guān)于連接與 Django 中查詢集中記錄的多行相關(guān)的字段的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!