Hello I am working on an Django Rest Framework api and one of the views is returning an empty list.
here's my view:
@api_view(['GET'])
def post_search(request):
form = SearchForm()
query = None
results = []
if request.method == 'GET':
if 'query' in request.GET:
form = SearchForm(request.GET)
if form.is_valid():
query = form.cleaned_data['query']
results = Paper.objects.annotate(
search=SearchVector('abstract', 'title'),).filter(search=query)
serializer = PaperSerializer(results, many=True)
return Response(serializer.data)
here is the form:
class SearchForm(forms.Form):
query = forms.CharField()
and here are my urls:
path('search/', views.post_search, name='post_search'),
So on the shell I ran:
Paper.objects.annotate(search=SearchVector('abstract', 'title'),).filter(search='Type')
and I got the results I wanted but when do this:
import requests
url = 'http://127.0.0.1:8000/api/search/?search=Type'
re = requests.get(url)
re.json # -> []
# or
import json
json.loads(re) # ->raise TypeError(f'the JSON object must be str, # bytes or bytearray, '
#TypeError: the JSON object must be str, bytes or bytearray, not Response
Any help will be appreciated; thanks
import pdb; pdb,set_trace(), afterif request.method == 'GET':and restart application and then hit the end point, in terminal run'query' in request.GET, if it is False then is root cause of your error'http://127.0.0.1:8000/api/search/?search=Type'That url does not contain aqueryarg, soif 'query' in request.GETwill be false.