Answers for "how do i check if a django queryset is empty"

1

how do i check if a django queryset is empty

queryset = demo.objects.filter(name="non_existent_name")

if queryset.exists():
    serializer = DemoSerializer(queryset, many=True)
    return Response(serializer.data)
else:
    return Response(status=status.HTTP_404_NOT_FOUND)
Posted by: Guest on June-23-2020

Code answers related to "how do i check if a django queryset is empty"

Python Answers by Framework

Browse Popular Code Answers by Language