跨Django的复杂查询反向泛型关系:可能吗?

时间:2013-10-18 06:22:34

标签: django django-orm generic-relations

我的课程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.

是不是可以这样查询?我可以做些什么呢?

1 个答案:

答案 0 :(得分:0)

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