Django prefetch_related with limit [英] Django prefetch_related with limit

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

问题描述

有没有办法告诉 prefetch_related 只能获取一组有限的相关对象?假设我正在获取用户列表,我知道我想要获取他们最近的评论。在循环中为每个用户提取注释而不是在获取用户时使用prefetch_related来预取它们。我的理解是,这将获取原始查询结果中的任何用户提供的所有评论,但我只想为每个用户显示最新的5。



如果评论列表真的很大,这会如何影响性能?有没有办法在单个(或2)查询中为每个用户提取5条评论?它不一定是原始的查询用于提取用户,但这将是不错的。



我本来想转一下这个

  users = User.objects .all()
用户的用户:
user.comments.all()[:10]

成这样的东西

  User.objects.all()。prefetch_related('comments',limit = 10)

所以如果用户有100或10000的注释,它们并不都被加载到内存中。您将如何在原始SQL中执行此操作?

解决方案

限制预取相关对象数量的唯一方法似乎是使用Prefetch()和过滤文件。使用分片

  User.objects.all()。prefetch_related(
Prefetch('msg_sent',queryset = UserMsg。 object.order_by(' - created')[:10]))

返回错误

  AssertionError:一旦切片被采取,就无法过滤查询。 

限制相关对象数量的唯一方法似乎是对值使用过滤器,例如timedelta
timelimit = datetime.now() - timedelta(days = 365)

$ b

 
$ b User.objects.all()。prefetch_related(
Prefetch('msg_sent',queryset = UserMsg.objects.filter(created__gte = timelimit)))

虽然这不返回固定数字,但在某些情况下可能会有用,并且会减少预取对象的数量。 >

Is there a way to tell prefetch_related to only fetch a limited set of related objects? Lets say I am fetching a list of users and I know I want to fetch their recent comments. Instead of fetching comments for each user in a loop, I use prefetch_related to pre-fetch them at the time of fetching the users. My understanding is that this will fetch all the comments made by any user present in the result of the original query but I only want to show the latest 5 for each user.

How does this affect the performance if the list of comments is really huge? Is there a way to fetch only 5 comments for each user in a single (or 2) query? It doesn't have to be the same query as the original one for fetching users but that would be nice.

I essentially want to turn this

   users = User.objects.all()
   for user in users:
       user.comments.all()[:10]

into something like this

 User.objects.all().prefetch_related('comments', limit=10)

so if a user has 100s or 10000s of comments, they are not all loaded into memory. How would you do something like this in raw SQL?

解决方案

The only way to limit the number of prefetched related objects seems to be using Prefetch() and filtering on fileds. Using sliceing

User.objects.all().prefetch_related(
    Prefetch('msg_sent', queryset=UserMsg.objects.order_by('-created')[:10]))

returns an error

AssertionError: Cannot filter a query once a slice has been taken.

The only way to limit the number of related objects seems to be using filter on a value, for example

from datetime import datetime, timedelta
timelimit = datetime.now() - timedelta(days=365)

User.objects.all().prefetch_related(
    Prefetch('msg_sent', queryset=UserMsg.objects.filter(created__gte=timelimit)))

While that doesn't return a fixed number, in may be useful in some situation, and it will reduce the number of prefetched objects.

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

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