如何一次性获取所有 django 外键对象?

发布于 2024-12-04 19:19:57 字数 851 浏览 0 评论 0原文

上下文:Foo 是一个具有指向 Bar 对象的foreignKey 字段的类。多个 Foo 可能引用同一个 Bar。 (多对一)

我需要引用 Foo 对象的外键的多个字段(Bar.fieldBar.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 Foos 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天涯离梦残月幽梦 2024-12-11 19:19:57
foo_list = Foo.objects.filter(params).select_related()

做这个工作。其余代码将保持不变,但

for f in foo_list:
    f.bar

将使用缓存的对象

foo_list = Foo.objects.filter(params).select_related()

does the job. The rest of the code would remain the same, but

for f in foo_list:
    f.bar

would use the cached objects

卷耳 2024-12-11 19:19:57

是的,您可以在查询集上使用 select_lated 方法 (https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet。 select_相关),它将在初始查询中为您执行连接。因此,在您的情况下,类似于:

foo_list = Foo.objects.filter(params).select_related()

如果您有一个非常深的链接模型链,那么也可能值得限制深度,以便您只获得栏对象(而不是除此之外的任何其他链接,例如 .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:

foo_list = Foo.objects.filter(params).select_related()

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)

走过海棠暮 2024-12-11 19:19:57

使用 关系 查找例如:

foo_list = Foo.objects.filter(params).values('field','bar__barField','bar__barField2')

这将在一个查询中完成这项工作,并且结果集也将包含所需的字段。

Use relationship look up's eg:

foo_list = Foo.objects.filter(params).values('field','bar__barField','bar__barField2')

This will do the job in one query and the result set will have the required fields as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文