简体   繁体   中英

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

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?

我最终使用了@Ahmad 在评论中介绍的帖子Stack-overflow 答案中解释的解决方案。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM