mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-12-17 21:56:36 +00:00
One query to get book for book view
This commit is contained in:
parent
35131262ff
commit
cb089ed817
1 changed files with 10 additions and 12 deletions
|
@ -38,18 +38,16 @@ class Book(View):
|
||||||
|
|
||||||
user_statuses = user_statuses if request.user.is_authenticated else False
|
user_statuses = user_statuses if request.user.is_authenticated else False
|
||||||
|
|
||||||
try:
|
# it's safe to use this OR because edition and work and subclasses of the same
|
||||||
book = models.Edition.viewer_aware_objects(request.user).filter(id=book_id)
|
# table, so they never have clashing IDs
|
||||||
except models.Edition.DoesNotExist:
|
book = (
|
||||||
book = (
|
models.Edition.viewer_aware_objects(request.user)
|
||||||
models.Edition.viewer_aware_objects(request.user)
|
.filter(Q(id=book_id) | Q(parent_work__id=book_id))
|
||||||
.filter(
|
.order_by("-edition_rank")
|
||||||
parent_work__id=book_id,
|
.select_related("parent_work")
|
||||||
)
|
.prefetch_related("authors")
|
||||||
.order_by("-edition_rank")
|
.first()
|
||||||
)
|
)
|
||||||
book = book.select_related("parent_work").prefetch_related("authors")
|
|
||||||
book = book.first()
|
|
||||||
|
|
||||||
if not book or not book.parent_work:
|
if not book or not book.parent_work:
|
||||||
raise Http404
|
raise Http404
|
||||||
|
|
Loading…
Reference in a new issue