17👍
✅
Here is the way to do it:
from django.core import serializers
def return_only_one_row():
return HttpResponse(serializers.serialize("json", Y.objects.filter(pk=1)))
Using filter()
instead of get()
returns the proper JSON response.
Another way to do it would be to use Python lists. You can wrap the query in [ ]
to turn the resulting response to a list and then serialize it to JSON. Example follows:
def return_only_one_row():
return HttpResponse(serializers.serialize("json", [Y.objects.get(pk=1)]))
Source:stackexchange.com