【问题标题】:Complex queries across Django reverse generic relations: possible?跨 Django 的复杂查询反向通用关系:可能吗?
【发布时间】:2013-10-18 06:22:33
【问题描述】:

我有一个班级Image,下面是GenericRelation

properties = models.GenericRelation(Property)

我正在尝试获取具有某些属性的所有图像,所以我这样做:

Image.objects.filter(properties__type = "foo", properties__user = request.user)

但这会导致以下错误:

DatabaseError: operator does not exist: integer = text
LINE 1: ...perties_property" ON ("myapp_image"."id" = "propert...
                                                          ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

不能这样查询吗?作为替代方案,我能做些什么?

【问题讨论】:

  • 你给了一个和平的错误;在这种情况下,我们可以给您一个安心的答案,但不确定是否可行;)您也应该向我们展示这 2 个模型

标签: django django-orm generic-relations


【解决方案1】:

如果您需要对泛型关系进行非常复杂的查询,那么我宁愿建议您自己编写 sql 并使用原始查询。所以我想我会说原始查询是另一种选择。

【讨论】:

    猜你喜欢
    • 2019-11-06
    • 2011-03-23
    • 2011-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-18
    • 2023-04-06
    相关资源
    最近更新 更多