Skip to content

Commit

Permalink
Use Case When
Browse files Browse the repository at this point in the history
  • Loading branch information
derneuere committed Mar 29, 2024
1 parent 6a9af51 commit 0cb12b2
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions api/views/albums.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import re

from django.db.models import Count, F, Prefetch, Q
from django.db.models import Case, Count, F, IntegerField, Prefetch, Q, When
from drf_spectacular.utils import OpenApiParameter, OpenApiTypes, extend_schema
from rest_framework import filters, viewsets
from rest_framework.permissions import AllowAny, IsAuthenticated
Expand Down Expand Up @@ -187,18 +187,26 @@ def get_queryset(self):
"image_hash", "video"
)

return (
queryset = (
AlbumThing.objects.filter(owner=self.request.user)
.annotate(photo_count=Count("photos", filter=Q(photos__hidden=False)))
.prefetch_related(
Prefetch(
"photos", queryset=cover_photos_query[:4], to_attr="cover_photos"
)
)
.annotate(
photo_count=Count(
Case(
When(photos__hidden=False, then=1), output_field=IntegerField()
)
)
)
.filter(photo_count__gt=0)
.order_by("-title")
)

return queryset


class AlbumPlaceViewSet(viewsets.ModelViewSet):
serializer_class = AlbumPlaceSerializer
Expand Down

0 comments on commit 0cb12b2

Please sign in to comment.