Answers for "django filter check if 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

Python Answers by Framework

Browse Popular Code Answers by Language