Hey guys, 

I need a help with a quirky bug while iterating over a QuerySet:

> TypeError: argument of type 'QuerySet' is not iterable


The following is the block of code that produces the error: (the save 
method overrides the default save method in a Model called *Transaction*)

def save(self, *args, **kwargs):
        obj = self
        if not obj.ref_number:  # the line of code throwing the error
            transactions = Transaction.objects.values_list("ref_number", 
flat=True)     # cache the list
            while True:
                ref_number = random.randint(1000000001, 9999999999)
                if ref_number not in transactions:
                    obj.ref_number = ref_number
                    super(Transaction, obj).save(*args, **kwargs)
                    return
        else:
            super(Transaction, obj).save(*args, **kwargs)


This piece of code was working fine until we had modified (what we thought 
to be) an unrelated part of the source: (this is the modified line of code)
items = OrderItem.objects.prefetch_related('toppings').select_related('item'
).select_related('item__category')
order = Order.objects.filter(uuid=order_uuid).prefetch_related(
            Prefetch(
                lookup='items',
                queryset=items
            )
        ).select_related('outlet').select_related('user').select_related(
'outlet__group') \
         .select_for_update(of=('self', 'outlet'))


We basically added a call to the *select_for_update. *This was required by 
us since we are modifying both the *Order *as well as *Outlet *models. 
(Also, the *Transaction* model is a super-model for the *Order *model)

My best guess is that the select_for_update locks certain rows of the the 
*Order*, *Transaction *and a bunch of other models. This results in the 
line of code failing because the query to list the value fails. However, I 
tested this hypothesis out by running a *select_for_update*, populating the 
*QuerySet 
*returned and then sleeping for a while while I ran a different query on 
the Model for which rows were locked. This ran perfectly fine, with no 
problems.

We are using PostgreSQL as our backend database. Any help would be greatly 
appreciated. 

Thanks

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/b3c57754-5b9a-4ce2-a24e-85b49c455eaf%40googlegroups.com.

Reply via email to