Answers for "django check empty queryset"

2

django empty queryset

Model.objects.none()
Posted by: Guest on May-05-2020
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
0

check if queryset is empty django template

<ul>
{% for athlete in athlete_list %}
    <li>{{ athlete.name }}</li>
{% empty %}
    <li>Sorry, no athletes in this list.</li>
{% endfor %}
</ul>
Posted by: Guest on September-28-2020

Python Answers by Framework

Browse Popular Code Answers by Language