Search code examples
djangodjango-modelsdjango-querysetdjango-orm

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

Solution

  • 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