Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - queryset disappears after update command

Tags:

django

I have a simple Django command like so:

class OrderQuerySet(models.query.QuerySet):
    def filter_vendor(self, vendor):
        return self.filter(agreement_vendors_merchandise_list__agreement_vendors__vendor = vendor)
    def suggestions(self, legal_entity_own):
        result = self.filter(status_id = 1, is_active_flg = 1)
        pending_orders = Order.objects.filter_legal_entity_own(legal_entity_own).\
                                       with_pending_status()
        vendor_id_list = pending_orders.values('agreement_vendors_merchandise_list__agreement_vendors__vendor').distinct()
        vendors = Vendors.objects.exclude(id__in = vendor_id_list) 
        result = result.filter_vendors(vendors) 
        return result

class Order(models.Model):
      objects = OrderQuerySet.as_manager()

def my_strange_behaviour_view
    suggested_orders_qs = Order.objects.suggestions(legal_entity_own_instance).\
                                            filter(chosen_quantity__gt = 0)    
    order_qs_by_vendor = suggested_orders_qs.filter_vendor(v)
    order_qs_by_vendor.update(status_id = 2) # This command makes the queryset empty

After this command, the querysets becomes empty ! I am just lost. And can't find any hint in documentation. Can anyone explain what's going on ?

like image 515
Edgar Navasardyan Avatar asked Oct 15 '16 16:10

Edgar Navasardyan


1 Answers

Your suggestions queryset method already filtered using status_id=1 so when you updated the queryset with status_id=2 the queryset simply does not hold further data because of filtering applied via status_id=1

In order to preserve you need a shallow copy of original queryset before update:

import copy
original_qs = copy.copy(order_qs_by_vendor)
order_qs_by_vendor.update(status_id=2)
# now try to do original_qs.count() but it will still hold the status_id=1 though
like image 124
Aamir Rind Avatar answered Nov 13 '22 03:11

Aamir Rind