如何一次性获取所有 django 外键对象?
上下文:Foo
是一个具有指向 Bar
对象的foreignKey 字段的类。多个 Foo
可能引用同一个 Bar
。 (多对一)
我需要引用 Foo
对象的外键的多个字段(Bar.field
和 Bar.field2
),但似乎每次我这样做时它都会运行一个单独的查询。
因此,引用这些字段可能会花费很长时间。
有没有办法在单个查询中获取所有 Bar
对象?或者有没有更好的方法来加快速度?
foo_list = Foo.objects.filter(params) # takes 0.001 sec, returns 10k objects
result = []
for f in foo_list:
# this part takes 40 seconds due to the huge number of queries
bar = f.bar # Foo contains a foreignKey reference to the Bar class
result.append({
"field": f.field,
"barField": bar.barField,
"barField2": bar.barField2,
# this way was even worse
# "barField": f.bar.barField,
# "barField2": f.bar.barField2,
})
Context: Foo
is a class that has a foreignKey field pointing to a Bar
object. Several Foo
s may refer to the same Bar
. (Many to one)
I need to reference several fields of a Foo
object's foreignkey (Bar.field
and Bar.field2
), but it seems that each time I do so it runs a separate query.
As a result, referencing these fields can take forever.
Is there a way to get all the Bar
objects in a single query? Or is there a better way to speed it up?
foo_list = Foo.objects.filter(params) # takes 0.001 sec, returns 10k objects
result = []
for f in foo_list:
# this part takes 40 seconds due to the huge number of queries
bar = f.bar # Foo contains a foreignKey reference to the Bar class
result.append({
"field": f.field,
"barField": bar.barField,
"barField2": bar.barField2,
# this way was even worse
# "barField": f.bar.barField,
# "barField2": f.bar.barField2,
})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
做这个工作。其余代码将保持不变,但
将使用缓存的对象
does the job. The rest of the code would remain the same, but
would use the cached objects
是的,您可以在查询集上使用
select_lated
方法 (https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet。 select_相关),它将在初始查询中为您执行连接。因此,在您的情况下,类似于:如果您有一个非常深的链接模型链,那么也可能值得限制深度,以便您只获得栏对象(而不是除此之外的任何其他链接,例如
.select_lated(深度=1)
Yes, you can use the
select_related
method on your queryset (https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.select_related), which will do the joins for you in the initial query. So in your case, something like:If you have a very deep chain of linked models, it may be worth also limiting the depth so that you only get the bar objects (and not any additional links beyond that like
.select_related(depth=1)
使用 关系 查找例如:
这将在一个查询中完成这项工作,并且结果集也将包含所需的字段。
Use relationship look up's eg:
This will do the job in one query and the result set will have the required fields as well.