Skip to content

Commit

Permalink
๐Ÿฉน Fix: ์šด์˜ ์ƒํƒœ์— ๋”ฐ๋ฅธ ๋ถ€์Šค ์ •๋ ฌ ์ถ”๊ฐ€
Browse files Browse the repository at this point in the history
  • Loading branch information
dudtlstm committed Oct 8, 2024
1 parent 74b15f8 commit ba94db1
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions booth/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from rest_framework import viewsets, status
from rest_framework.decorators import action
from rest_framework.response import Response
from django.db.models import Count
from django.db.models import Case, When, Value, IntegerField, Count
from rest_framework.filters import OrderingFilter
from .models import Booth
from .serializers import BoothListSerializer, BoothDetailSerializer
Expand All @@ -12,18 +12,39 @@

class BoothViewSet(CustomResponseMixin, viewsets.GenericViewSet, mixins.RetrieveModelMixin, mixins.ListModelMixin):
queryset = Booth.objects.all()
filter_backends = [OrderingFilter]
ordering_fields = ['name', 'waiting_count']
ordering = ['name'] # ๋””ํดํŠธ ์ •๋ ฌ: ๋ถ€์Šค๋ช… ๊ฐ€๋‚˜๋‹ค์ˆœ
# filter_backends = [OrderingFilter]
# ordering_fields = ['name', 'waiting_count', 'is_operated']
# ordering = ['name'] # ๋””ํดํŠธ ์ •๋ ฌ: ๋ถ€์Šค๋ช… ๊ฐ€๋‚˜๋‹ค์ˆœ

def get_serializer_class(self):
if self.action == 'list':
return BoothListSerializer
return BoothDetailSerializer

def get_queryset(self):
queryset = Booth.objects.annotate(waiting_count=Count('waitings'))
return queryset
# ์šด์˜ ์ƒํƒœ์— ๋”ฐ๋ฅธ ์ •๋ ฌ ์šฐ์„ ์ˆœ์œ„ ์„ค์ • ! '์šด์˜ ์ค‘ - ๋Œ€๊ธฐ ์ค‘์ง€ - ์šด์˜ ์ „ - ์šด์˜์ข…๋ฃŒ' ์ˆœ
queryset = Booth.objects.annotate(
waiting_count=Count('waitings'),
is_operated_order=Case(
When(is_operated='operating', then=Value(1)),
When(is_operated='paused', then=Value(2)),
When(is_operated='not_started', then=Value(3)),
When(is_operated='finished', then=Value(4)),
output_field=IntegerField()
)
)

# ordering ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ waiting_count์ผ ๊ฒฝ์šฐ '์šด์˜ ์ƒํƒœ'๋กœ ์ •๋ ฌ ํ›„ '๋Œ€๊ธฐ ์ˆœ'์œผ๋กœ ์ •๋ ฌ
ordering = self.request.query_params.get('ordering')
if ordering == 'waiting_count':
return queryset.order_by('is_operated_order', 'waiting_count')
elif ordering == '-waiting_count':
return queryset.order_by('is_operated_order', '-waiting_count')
elif ordering == 'name':
return queryset.order_by('is_operated_order', 'name')

# ๋””ํดํŠธ: ์šด์˜ ์ƒํƒœ + ์ด๋ฆ„ ์ˆœ ์ •๋ ฌ
return queryset.order_by('is_operated_order', 'name')

# def create(self, request, *args, **kwargs):
# serializer = self.get_serializer(data=request.data)
Expand Down

0 comments on commit ba94db1

Please sign in to comment.