localhost:8000/places/search?region=제주시&category=카페&category=한식&limit=3
제주시
이면서카페
나 한식
이어야 한다.places_region
이 애월 · 한림
가 나온다.제주시
이어야 하는데...{
"results": [
{
"place_id": 1,
"place_name": "토투가커피",
"place_opening_hours": "10:00 - 18:00, 연중무휴",
"place_maximum_number_of_subscriber": 0,
"place_able_to_reserve": false,
"place_closed_temporarily": false,
"place_category": "카페",
"place_region": "애월 · 한림",
...
},
{
"place_id": 2,
"place_name": "꽃가람",
"place_opening_hours": "09:10 - 20:00, 매 주 목요일 휴무",
"place_maximum_number_of_subscriber": 6,
"place_able_to_reserve": true,
"place_closed_temporarily": false,
"place_category": "한식",
"place_region": "성산 · 표선",
...
},
{
"place_id": 3,
"place_name": "라스또르따스",
"place_opening_hours": "11:00 - 16:00 (금, 토요일 : 11:00 - 21:00), 매 주 월요일, 화요일 휴무",
"place_maximum_number_of_subscriber": 8,
"place_able_to_reserve": true,
"place_closed_temporarily": false,
"place_category": "양식",
"place_region": "제주시",
...
}
]
}
그렇다면 기존 views.py
를 보자
1. Q 객체
에 regions
와 categories
가 모두 |=
형태로 쌓이기 때문에 발생하는 것 같다.
2. 그렇다면 Q 객체
에 대한 변수인 q
말고 다른 변수를 하나 더 선언해야 한다.
class PlaceSearchView(View):
def get(self, request):
try:
regions = request.GET.getlist('region')
categories = request.GET.getlist('category')
sort = request.GET.get('sort')
offset = int(request.GET.get('offset', 0))
limit = int(request.GET.get('limit', 20))
q = Q()
if regions:
for region in regions:
q |= Q(region__name = region)
if categories:
for category in categories:
q |= Q(category__name = category)
.
.
.
places = Place.objects.annotate(
min_price = Min('placemenu__price__price'),
max_price = Max('placemenu__price__price'),
avg_price = Avg('placemenu__price__price')
).filter(q).order_by(order_key)[offset : offset + limit]
.
.
.
return JsonResponse({'results' : results}, status = 200)
except KeyError:
return JsonResponse({'message' : 'KEY_ERROR'}, status = 400)
q
를 분리하여 q_region
와 q_category
로 분리하였다.filter()
조건에 q_region & q_category
의 형태로 넣었다.class PlaceSearchView(View):
def get(self, request):
try:
regions = request.GET.getlist('region')
categories = request.GET.getlist('category')
sort = request.GET.get('sort')
offset = int(request.GET.get('offset', 0))
limit = int(request.GET.get('limit', 20))
q_region = Q()
q_category = Q()
if regions:
for region in regions:
q_region |= Q(region__name = region)
if categories:
for category in categories:
q_category |= Q(category__name = category)
.
.
.
places = Place.objects.annotate(
min_price = Min('placemenu__price__price'),
max_price = Max('placemenu__price__price'),
avg_price = Avg('placemenu__price__price')
).filter(q_region & q_category).order_by(order_key)[offset : offset + limit]
.
.
.
return JsonResponse({'results' : results}, status = 200)
except KeyError:
return JsonResponse({'message' : 'KEY_ERROR'}, status = 400)
localhost:8000/places/search?region=제주시&category=카페&category=한식&limit=3
🎉🎉🎉이제 제주시만 나온다!!!🎉🎉🎉
{
"results": [
{
"place_id": 9,
"place_name": "일통이반",
"place_opening_hours": "12:00 - 00:00, 매 월 2, 4주 화요일 휴무",
"place_maximum_number_of_subscriber": 8,
"place_able_to_reserve": true,
"place_closed_temporarily": false,
"place_category": "한식",
"place_region": "제주시"
...
},
{
"place_id": 11,
"place_name": "숙성도",
"place_opening_hours": "11:00 - 21:00, 연중무휴",
"place_maximum_number_of_subscriber": 0,
"place_able_to_reserve": false,
"place_closed_temporarily": false,
"place_category": "한식",
"place_region": "제주시"
...
},
{
"place_id": 12,
"place_name": "우진해장국",
"place_opening_hours": "06:00 - 22:00, 연중무휴",
"place_maximum_number_of_subscriber": 10,
"place_able_to_reserve": true,
"place_closed_temporarily": false,
"place_category": "한식",
"place_region": "제주시"
...
}
]
}