Magesh Ravi

Magesh Ravi

Artist | Techie | Entrepreneur

Updating a queryset empties values-list in Django ORM

I noticed something weird in the Django ORM while using queryset.values_list() and queryset.update() together.

from subscriptions.models import PurchaseOrder

# get queryset
purchase_orders = PurchaseOrder.objects.filter(status=PurchaseOrder.Status.HALTED)

# get values-queryset
po_values_list = purchase_orders.values_list("razorpay_order_id", flat=True)

print(po_values_list)   # prints non-empty queryset

# update using queryset.update() resets the values_list
purchase_orders.update(status=PurchaseOrder.Status.CREATED)

print(po_values_list)   # prints empty queryset

What's happening?

  1. I have a queryset.
  2. I evaluate/derive a values-list from the queryset.
  3. Updating the queryset empties the values-list. Irrespective of whether I use queryset.update() or instance.update() within a for-loop, this happens.

I haven't tried picking the values-list. I'm still trying to understand why this is happening.

Last updated: May 10, 2024, 10:34 a.m.