django queryset empty code example

Example 1: django empty queryset

Model.objects.none()

Example 2: 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 3: checking if a queryset is empty django

orgs = Organisation.objects.filter(name__iexact = 'Fjuk inc')
if not orgs:# If any results
    # Do this with the results without querying again.
else:# Else, do something else...
    # Do that...

Example 4: 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>