Ali Raza
Ali Raza

Reputation: 1073

Django check if query-set response is empty

I want to check if items queryset is empty or not.

order = Order.objects.get(customer=request.user.customer)
items = order.orderitem_set.all() #check if items length is 0

Upvotes: 2

Views: 2496

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477881

You can check this with .exists() [Django-doc]:

order = Order.objects.get(customer=request.user.customer, complete=False)
flag = order.orderitem_set.exists()  # True if at least one item

Upvotes: 4

Related Questions