Django:select_related和GenericRelation [英] Django: select_related and GenericRelation

查看:507
本文介绍了Django:select_related和GenericRelation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

选择相关工作是否与GenericRelation关系有关,还是有合理的选择?在这一刻,Django做个别的sql调用了我的查询集中的每个项目,我想避免使用类似于select_related的东西。

 类声明(models.Model):
proof = generic.GenericRelation(Proof)


class Proof(models.Model):
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type' ,'object_id')

我正在选择一堆索赔,我想要相关证明被拉入而不是单独查询。

解决方案

看起来像 select_related和GRs不一起工作。我想你可以编写一些用于声明的访问器,通过相同的查询获取它们。这个帖子给你一些关于原始SQL的指针通用对象,如果您需要它们


Does select_related work for GenericRelation relations, or is there a reasonable alternative? At the moment Django's doing individual sql calls for each item in my queryset, and I'd like to avoid that using something like select_related.

class Claim(models.Model):
    proof = generic.GenericRelation(Proof)


class Proof(models.Model):
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')

I'm selecting a bunch of Claims, and I'd like the related Proofs to be pulled in instead of queried individually.

解决方案

Looks like select_related and GRs don't work together. I guess you could write some kind of accessor for Claim that gets them all via the same query. This post gives you some pointers on raw SQL to get generic objects, if you need them

这篇关于Django:select_related和GenericRelation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆