def get_queryset(self): search_text = self.request.query_params.get("search_text") queryset = self.queryset if search_text: vector = SearchVector("name", "district__name", "state__name") query = SearchQuery(get_psql_search_tokens(search_text), search_type="raw") queryset = (self.queryset.annotate( search_text=vector, rank=SearchRank( vector, query)).filter(search_text=query).order_by("-rank")) return queryset
def filter_queryset(self, request, queryset, view): if request.user.is_superuser or request.query_params.get("all") == "true": pass elif request.user.user_type >= User.TYPE_VALUE_MAP["DistrictLabAdmin"]: queryset = queryset.filter(district=request.user.district) else: queryset = queryset.filter(Q(created_by=request.user) | Q(users__id__exact=request.user.id)) search_text = request.query_params.get("search_text") if search_text: vector = SearchVector("name", "district__name", "state__name") query = SearchQuery(get_psql_search_tokens(search_text), search_type="raw") queryset = ( queryset.annotate(search_text=vector, rank=SearchRank(vector, query)) .filter(search_text=query) .order_by("-rank") ) return queryset