Django SQL查询重复了n次 [英] Django SQL query duplicated n times

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

问题描述

我有一个书模型和一个评级模型,

I have a book model and a rating model,

class Book(models.Model):
    title = models.CharField(max_length=255)
    slug = AutoSlugField(unique=True, populate_from='title')
    description = models.TextField()
    # more fields

class Rating(models.Model):
    book = models.ForeignKey('library.Book')
    score = models.DecimalField(max_digits=2, decimal_places=1)

查询,

books = {'books': Book.objects.filter(pk__in=Rating.objects.all().order_by('-score'
              ).values_list('book__id', flat=True))[:10] }

模板,

{% for i in books %}
   {{ i.title }}, {{ i.rating_set.all.first.score }} <br/>
{% endfor %}

将模型提供给模板,但是django调试工具栏显示为重复n次,其中n是列表中的对象数。当我使用查询集缓存时,它是正常的。

renders the model to the template, but the django debug toolbar shows as Duplicated n times where n is the number of objects in the list. when I use queryset caching, its normal.

发生了什么事,我该如何解决?

whats going on behind, how can I fix this?

谢谢。

推荐答案

没有测试,但您绝对应该预取 rating_set 不会使每本书的额外数据库命中率达到最高分:

Didn't test but you should definitely prefetch rating_set to not make additional database hit for each book to find their highest score:

rated_books = Rating.objects.all().order_by('-score').values_list('book', flat=True)
books = Book.objects.prefetch_related('rating_set').filter(pk__in=rated_books)[:10]

在模板中,我还怀疑 .first .all ,因为它们可能会导致额外的数据库命中。此外,您不需要调用 .first ,因为我们已经知道这些评级书具有至少一个评级对象。

In the template, I also suspect .first and .all as they may cause an additional db hit. Besides, you don't need to call .first because we already know these rated books have at least one rating object.

{% for book in books %}
  {{ book.title }}, {{ book.rating_set.all.0.score }} <br/>
{% endfor %}

更新:您需要使用 rating_set.all.0 代替 rating_set.0 进行一流的选择

Update: You need to use rating_set.all.0 instead of rating_set.0 to selec first rate

这篇关于Django SQL查询重复了n次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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