如何在django中的特定链式prefetch_recyated()上应用任意过滤器?

发布于 2025-01-29 02:53:04 字数 2710 浏览 3 评论 0原文

我正在尝试优化API的射击查询。我有四个模型,即用户,内容,评级和用户,并彼此之间有一些关系。我希望相应的API返回所有现有内容以及其评分计数以及特定用户给出的分数。

我曾经做过这样的事情:content.objects.all()作为QuerySet,但我意识到,在有大量数据吨的情况下,将发出大量查询。因此,我已经尽力使用select_reled()prefetch_relelated()来优化射击查询。但是,我正在处理额外的python搜索,我希望使用受控的prefetch_reled() - 仅适用于特定的prefetch嵌套预摘要选择

这是我的模型:

from django.db import models
from django.conf import settings

class Content(models.Model):
    title = models.CharField(max_length=50)

class Rating(models.Model):
    count = models.PositiveBigIntegerField(default=0)
    content = models.OneToOneField(Content, on_delete=models.CASCADE)

class UserRating(models.Model):
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL, blank=True, null=True, on_delete=models.CASCADE
    )
    score = models.PositiveSmallIntegerField()
    rating = models.ForeignKey(
        Rating, related_name="user_ratings", on_delete=models.CASCADE
    )

    class Meta:
        unique_together = ["user", "rating"]

这是我到目前为止所做的:

contents = (
    Content.objects.select_related("rating")
    .prefetch_related("rating__user_ratings")
    .prefetch_related("rating__user_ratings__user")
)

for c in contents:  # serializer like
    user_rating = c.rating.user_ratings.all()
    for u in user_rating:  # how to remove this dummy search?
        if u.user_id == 1:
            print(u.score)

查询:

(1) SELECT "bitpin_content"."id", "bitpin_content"."title", "bitpin_rating"."id", "bitpin_rating"."count", "bitpin_rating"."content_id" FROM "bitpin_content" LEFT OUTER JOIN "bitpin_rating" ON ("bitpin_content"."id" = "bitpin_rating"."content_id"); args=(); alias=default
(2) SELECT "bitpin_userrating"."id", "bitpin_userrating"."user_id", "bitpin_userrating"."score", "bitpin_userrating"."rating_id" FROM "bitpin_userrating" WHERE "bitpin_userrating"."rating_id" IN (1, 2); args=(1, 2); alias=default
(3) SELECT "users_user"."id", "users_user"."password", "users_user"."last_login", "users_user"."is_superuser", "users_user"."first_name", "users_user"."last_name", "users_user"."email", "users_user"."is_staff", "users_user"."is_active", "users_user"."date_joined", "users_user"."user_name" FROM "users_user" WHERE "users_user"."id" IN (1, 4); args=(1, 4); alias=default

如上所述的查询,您只有三个查询,而不是过去发生的问题。但是,我想我可以在我的最新查询中使用过滤器删除Python搜索(循环的第二个) - users_user”。“ ID”(1,)而不是。发布和我的努力,我无法应用.filter(rating__user_ratings__user_id = 1)在第三个查询上,我无法使用prefetch(。 。

I'm trying to optimize the fired queries of an API. I have four models namely User, Content, Rating, and UserRating with some relations to each other. I want the respective API returns all of the existing contents alongside their rating count as well as the score given by a specific user to that.

I used to do something like this: Content.objects.all() as a queryset, but I realized that in the case of having a huge amount of data tons of queries will be fired. So I've done some efforts to optimize the fired queries using select_related() and prefetch_related(). However, I'm dealing with an extra python searching, that I hope to remove that, using a controlled prefetch_related() — applying a filter just for a specific prefetch in a nested prefetch and select.

Here are my models:

from django.db import models
from django.conf import settings

class Content(models.Model):
    title = models.CharField(max_length=50)

class Rating(models.Model):
    count = models.PositiveBigIntegerField(default=0)
    content = models.OneToOneField(Content, on_delete=models.CASCADE)

class UserRating(models.Model):
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL, blank=True, null=True, on_delete=models.CASCADE
    )
    score = models.PositiveSmallIntegerField()
    rating = models.ForeignKey(
        Rating, related_name="user_ratings", on_delete=models.CASCADE
    )

    class Meta:
        unique_together = ["user", "rating"]

Here's what I've done so far:

contents = (
    Content.objects.select_related("rating")
    .prefetch_related("rating__user_ratings")
    .prefetch_related("rating__user_ratings__user")
)

for c in contents:  # serializer like
    user_rating = c.rating.user_ratings.all()
    for u in user_rating:  # how to remove this dummy search?
        if u.user_id == 1:
            print(u.score)

Queries:

(1) SELECT "bitpin_content"."id", "bitpin_content"."title", "bitpin_rating"."id", "bitpin_rating"."count", "bitpin_rating"."content_id" FROM "bitpin_content" LEFT OUTER JOIN "bitpin_rating" ON ("bitpin_content"."id" = "bitpin_rating"."content_id"); args=(); alias=default
(2) SELECT "bitpin_userrating"."id", "bitpin_userrating"."user_id", "bitpin_userrating"."score", "bitpin_userrating"."rating_id" FROM "bitpin_userrating" WHERE "bitpin_userrating"."rating_id" IN (1, 2); args=(1, 2); alias=default
(3) SELECT "users_user"."id", "users_user"."password", "users_user"."last_login", "users_user"."is_superuser", "users_user"."first_name", "users_user"."last_name", "users_user"."email", "users_user"."is_staff", "users_user"."is_active", "users_user"."date_joined", "users_user"."user_name" FROM "users_user" WHERE "users_user"."id" IN (1, 4); args=(1, 4); alias=default

As you can see on the above fired queries I've only three queries rather than too many queries which were happening in the past. However, I guess I can remove the python searching (the second for loop) using a filter on my latest query — users_user"."id" IN (1,) instead. According to this post and my efforts, I couldn't apply a .filter(rating__user_ratings__user_id=1) on the third query. Actually, I couldn't match my problem using Prefetch(..., queryset=...) instance given in this answer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

夜访吸血鬼 2025-02-05 02:53:04

我认为您正在寻找预取对象:
https:// https:///djangoproject.com/en/4.0 /ref/models/querySets/#prefetch-objects

尝试以下操作:

from django.db.models import Prefetch

contents = Content.objects.select_related("rating").prefetch_related(
    Prefetch(
        "rating__user_ratings",
        queryset=UserRating.objects.filter(user__id=1),
        to_attr="user_rating_number_1",
    )
)

for c in contents:  # serializer like
    print(c.rating.user_rating_number_1[0].score)

I think you are looking for Prefetch object:
https://docs.djangoproject.com/en/4.0/ref/models/querysets/#prefetch-objects

Try this:

from django.db.models import Prefetch

contents = Content.objects.select_related("rating").prefetch_related(
    Prefetch(
        "rating__user_ratings",
        queryset=UserRating.objects.filter(user__id=1),
        to_attr="user_rating_number_1",
    )
)

for c in contents:  # serializer like
    print(c.rating.user_rating_number_1[0].score)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文