Query efficiency in user view

This commit is contained in:
Mouse Reeve 2021-09-20 15:25:29 -07:00
parent aa0ffd86c0
commit 89fad59793
2 changed files with 15 additions and 5 deletions

View file

@ -12,7 +12,7 @@ register = template.Library()
def get_rating(book, user):
"""get the overall rating of a book"""
queryset = views.helpers.privacy_filter(
user, models.Review.objects.filter(book__in=book.parent_work.editions.all())
user, models.Review.objects.filter(book__parent_work__editions=book)
)
return queryset.aggregate(Avg("rating"))["rating__avg"]
@ -73,7 +73,7 @@ def active_shelf(context, book):
shelf = (
models.ShelfBook.objects.filter(
shelf__user=context["request"].user,
book__in=book.parent_work.editions.all(),
book__parent_work__editions=book,
)
.select_related("book", "shelf")
.first()
@ -97,4 +97,4 @@ def mutuals_count(context, user):
viewer = context["request"].user
if not viewer.is_authenticated:
return None
return user.followers.filter(id__in=viewer.following.all()).count()
return user.followers.filter(followers=viewer).count()

View file

@ -59,8 +59,18 @@ class User(View):
request.user,
user.status_set.select_subclasses(),
)
.select_related("reply_parent")
.prefetch_related("mention_books", "mention_users")
.select_related(
"user",
"reply_parent",
"review__book",
"comment__book",
"quotation__book",
)
.prefetch_related(
"mention_books",
"mention_users",
"attachments",
)
)
paginated = Paginator(activities, PAGE_LENGTH)