Skip to content

Python code snippet – 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)
See also  Python code snippet - How to test wifi speed py?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.