Django-taggit prefetch_related [英] Django-taggit prefetch_related

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

问题描述

我正在构建一个基本的时间记录应用程序,我有一个使用django-taggit的todo模型。我的Todo模型看起来像这样:

 类Todo(models.Model):
project = models.ForeignKey(Project )
description = models.CharField(max_length = 300)
is_done = models.BooleanField(default = False)
billable = models.BooleanField(default = True)
date_completed = models .DateTimeField(blank = True,null = True)
completed_by = models.ForeignKey(User,blank = True,null = True)
tags = TaggableManager()

def __unicode__ (自):
return self.description

我正在尝试获取一份项目中所有Todos的独特标签,我已经设法让这个工作使用集合的理解,但是对于项目中的每个Todo,我必须查询数据库以获取标签。我的设置理解是:在project.todo_set.all()中为

  unique_tags = {tag.name.lower()fordodo标签在todo.tags.all()} 

这个工作很好,但是对于每一个项目运行一个单独的查询来抓取所有的标签。我想知道是否有任何方式可以做类似于prefetch_related的事情,以避免这些重复的查询:

  unique_tags = { tag.name.lower()for project.todo_set.all()。prefetch_related('tags')forto.tags.all()} 

运行以前的代码给我错误:

 'tags'不解决支持预取的项目 - 这是prefetch_related()的无效参数。 

我在这里看到有人问了一个非常类似的问题:优化django查询以拉取外键和django-taggit关系,但是它没有看起来好像有明确的答案。我希望有人可以帮助我。谢谢!

解决方案

Taggit现在支持 prefetch_related 直接在标签字段版本0.11.0及更高版本,2013-11-25发布)。



此功能在此拉请求。在测试用例中,请注意,在预取后标签使用 .prefetch_related('tags'),还有0个额外的查询列表标签。


I'm building a basic time logging app right now and I have a todo model that uses django-taggit. My Todo model looks like this:

class Todo(models.Model):
    project = models.ForeignKey(Project)
    description = models.CharField(max_length=300)
    is_done = models.BooleanField(default=False)
    billable = models.BooleanField(default=True)
    date_completed = models.DateTimeField(blank=True, null=True)
    completed_by = models.ForeignKey(User, blank=True, null=True)
    tags = TaggableManager()

    def __unicode__(self):
        return self.description

I'm trying to get a list of unique tags for all the Todos in a project and I have managed to get this to work using a set comprehension, however for every Todo in the project I have to query the database to get the tags. My set comprehension is:

unique_tags = { tag.name.lower() for todo in project.todo_set.all() for tag in todo.tags.all() }

This works just fine, however for every todo in the project it runs a separate query to grab all the tags. I was wondering if there is any way I can do something similar to prefetch_related in order to avoid these duplicate queries:

unique_tags = { tag.name.lower() for todo in project.todo_set.all().prefetch_related('tags') for tag in todo.tags.all() }

Running the previous code gives me the error:

'tags' does not resolve to a item that supports prefetching - this is an invalid parameter to prefetch_related().

I did see that someone asked a very similar question here: Optimize django query to pull foreign key and django-taggit relationship however it doesn't look like it ever got a definite answer. I was hoping someone could help me out. Thanks!

解决方案

Taggit now supports prefetch_related directly on tag fields (in version 0.11.0 and later, released 2013-11-25).

This feature was introduced in this pull request. In the test case for it, notice that after prefetching tags using .prefetch_related('tags'), there are 0 additional queries for listing the tags.

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

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