Concatenation of a field related to multiple rows of a record in query set in Django

荒凉一梦 提交于 2019-12-02 03:53:00

问题


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')
)

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?


回答1:


I ended up using the solution explained in this Stack-overflow answer in the post introduced by @Ahmad in comments.



来源:https://stackoverflow.com/questions/48017719/concatenation-of-a-field-related-to-multiple-rows-of-a-record-in-query-set-in-dj

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!