django orm query if no result try another query code example
Example 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)
Example 2: django filter values with OR operator
Blog.objects.filter(pk__in=[1, 4, 7])