class SearchView(views.APIView):
serializer_class = BoothListSerializer
def get(self, request):
user = request.user
keyword= request.GET.get('keyword')
booths = (Booth.objects.filter(name__icontains=keyword) | Booth.objects.filter(menus__menu__contains=keyword)).distinct()
if user:
for booth in booths:
if booth.like.filter(pk=user.id).exists():
booth.is_liked=True
serializer = self.serializer_class(booths, many=True)
return Response({'message':'부스 검색 성공', 'data': serializer.data}, status=HTTP_200_OK)
keyword= request.GET.get('keyword') 리퀘스트에서 키워드 가져옴
booths = (Booth.objects.filter(name__icontains=keyword) | Booth.objects.filter(menus__menu__contains=keyword)).distinct()
부스 모델에서 name이나 menus의 menu필드에 키워드 들어가는거 필터링,
icontains: 대소문자 구분 없이 특정 문자열이 필드에 포함되어 있는지 검색
contains: 대소문자를 구분하여 특정 문자열이 필드에 포함되어 있는지 검색
distinct() : Django 쿼리셋에서 중복된 결과를 제거
if user: 로그인한 유저가 있으면
if booth.like.filter(pk=user.id).exists(): 부스의 like필드에 현재 로그인한 유저가 존재하면
booth.is_liked=True 부스가 좋아요 눌렸다고 직접 입력
serializer = self.serializer_class(booths, many=True) 부스 모델 이제 시리얼라이저에 넣는다
class SearchView(views.APIView):
def get(self, request):
keyword= request.GET.get('keyword')
posts = (Post.objects.filter(title__icontains=keyword) | Post.objects.filter(hashtag__hashtag__icontains=keyword) | Post.objects.filter(post_user__nickname__icontains=keyword)).distinct()
for post in posts:
post.author=post.post_user.nickname
if Vote.objects.filter(vote_post=post.post_id).exists():
post.is_vote=True
lines=Line.objects.filter(line_post=post.post_id).all()
if Question.objects.filter(que_line__in=lines).exists():
post.is_que=True
if Debate.objects.filter(debate_line__in=lines).exists():
post.is_debate=True
serializer = PostSearchSerializer(posts, many=True)
return Response({'message':'검색 성공', 'data': {"POST": serializer.data}}, status=status.HTTP_200_OK)