我必须用一对多的关系建模,我试图用它来区分我的记录类型.假设第一个模型专用于 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模板网!
获取所有产品、类别和元数据的 SQL 查询 woocommSQL query to get all products, categories and meta data woocommerce/wordpress(获取所有产品、类别和元数据的 SQL 查询 woocommerce/wordpre
如何在 WSL(Linux 的 Windows 子系统)中使用 MySQL?How to use MySQL in WSL (Windows Subsystem for Linux)?(如何在 WSL(Linux 的 Windows 子系统)中使用 MySQL?)
任务计划程序中的 PowerShell MySQL 备份脚本错误 PowerShell MySQL Backup Script Error in Task Scheduler 0x00041301(任务计划程序中的 PowerShell MySQL 备份脚本错误 0x00041301)
将数据从 XML 文件导入 MySQL 数据库Import the data from the XML files into a MySQL database(将数据从 XML 文件导入 MySQL 数据库)
在 Windows 7 32 位上安装 Xampp.启动时的错误installed Xampp on Windows 7 32-bit. Errors when starting(在 Windows 7 32 位上安装 Xampp.启动时的错误)
Windows xampp 上的 Mysql 小写表Mysql lower case table on Windows xampp(Windows xampp 上的 Mysql 小写表)