#26600: map says a queryset is not iterable
-------------------------------------+-------------------------------------
     Reporter:  ihucos               |                    Owner:  nobody
         Type:  Bug                  |                   Status:  closed
    Component:  Database layer       |                  Version:  1.11
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:  wontfix
     Keywords:  queryset iterator    |             Triage Stage:
  map                                |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Abhijeet):

 I'm unsure if bumping this up is against forum rules. However, I get this
 Error in Py3k in production. I'm not using `map` in my code, though I'm
 unsure if Django is internally using it.

 This is the code block that produces the error. The save method is
 overriding the save method of a Mode called `Transaction`.
 {{{
 def save(self, *args, **kwargs):
         obj = self
         if not obj.ref_number:  # Set the reference number before saving
             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)
 }}}

 The erroring line of code is:  `if ref_number not in transactions:`
 This piece of code was working fine until we had modified another
 unrelated part of the source 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'))
 }}}

 What we did was add the call to `select_for_update`, followed by a
 transaction which modifies the `Order` and `Outlet` model. Here, the
 `Order` model is a child model of the `Transaction` model.

 My guess as to what the problem is is that `select_for_update` locks the
 relevant rows/table which results in the line of code in the `save` method
 to fail internally, ultimately being caught as a cryptic type error. I'll
 be modifying
 `transactions = Transaction.objects.values_list("ref_number", flat=True)`
 to
 `transactions = Transaction.objects.values_list("ref_number",
 flat=True).list()` to see if that'll fix the problem.

 PS: I'm sorry if my formatting/post/bump is against norms. I'm new here,
 with this being my first comment.

 Replying to [comment:8 Simon Charette]:
 > I think we can close as ''wontfix'' given `map` doesn't turn
 `__iter__()` exceptions into `TypeError` on Py3k and we don't support Py2k
 anymore.
 >
 > Thanks for the investigation Vitaliy.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/26600#comment:13>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/064.e4f656ea2c60df09664242a590ff4b8f%40djangoproject.com.

Reply via email to