﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
26600	map says a queryset is not iterable	ihucos	nobody	"

So I got a funny bug with iterators map and Django that I can not localize or reproduce.
In our production server we get this sometimes, mainly for only one customer and it only fails on a very small percentage of all requests.

Sorry for only chunks of information, not sure if it is enough to process this bug request:

Th error message:
{{{
TypeError: argument 2 to map() must support iteration
}}}

Chunk of the code where this happens:
{{{#!python
            if filter(
                    lambda obj:
                    self.pk in verbose_map(attrgetter('pk'),
                                           obj.resources.all()) and
                    obj.begins < end and obj.ends > dt, resource_override_qs):
                return 0
}}}

Trying to reconstruct resource_override_qs I come up with:
{{{#!python
facility = Facility.objects.\
                prefetch_related('resources',
                                 'opening_hours',
                                 'openinghour_overrides',
                                 'slow_hours').\
                get(pk=facility.pk)
 resource_override_qs = facility.facilityresourceoverride_set.prefetch_related('resources')
}}}

What I did was to replace map with verbose_map in our production environment.
{{{#!python
def verbose_map(function, iterable):
    try:
        return map(function, iterable)
    except TypeError, exc:
        raise TypeError('map failed: ""{}"". iterable: {}, type: {}, attrs: {}'.format(  # NOQA
            exc, iterable, type(iterable), dir(iterable)))
}}}

After two weeks or so the error occurred again:
{{{#!python
map failed: ""argument 2 to map() must support iteration"". iterable: [<FacilityResource: 4>, <FacilityResource: 5>, <FacilityResource: 6>, <FacilityResource: 7>], type: <class 'django.db.models.query.QuerySet'>, attrs: ['__and__', '__bool__', '__class__', '__deepcopy__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__getitem__', '__getstate__', '__hash__', '__init__', '__iter__', '__len__', '__module__', '__new__', '__nonzero__', '__or__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_add_hints', '_as_sql', '_batched_insert', '_clone', '_create_object_from_params', '_db', '_earliest_or_latest', '_extract_model_params', '_fetch_all', '_filter_or_exclude', '_for_write', '_has_filters', '_hints', '_insert', '_known_related_objects', '_merge_known_related_objects', '_merge_sanity_check', '_next_is_sticky', '_populate_pk_values', '_prefetch_done', '_prefetch_related_lookups', '_prefetch_related_objects', '_prepare', '_raw_delete', '_result_cache', '_setup_aggregate_query', '_sticky_filter', '_update', 'aggregate', 'all', 'annotate', 'as_manager', 'bulk_create', 'complex_filter', 'count', 'create', 'dates', 'datetimes', 'db', 'defer', 'delete', 'distinct', 'earliest', 'exclude', 'exists', 'extra', 'filter', 'first', 'get', 'get_or_create', 'in_bulk', 'is_compatible_query_object_type', 'iterator', 'last', 'latest', 'model', 'none', 'only', 'order_by', 'ordered', 'prefetch_related', 'query', 'raw', 'reverse', 'select_for_update', 'select_related', 'update', 'update_or_create', 'using', 'value_annotation', 'values', 'values_list']
}}}
As we can see map really gets an QuerySet object, that queryset object does reveal its content in __str__ but python's built in map says its not iterable nevertheless
Alone for the customer where this happens the most that part of the code should be called at least a couple of times per day.

I am going with a workaround like calling list() on the queryset before mapping it or so.
But my company should be ok, to try this again with another verbose_map that may provide more debugging information. My efforts to reproduce this in our code base failed."	Bug	closed	Database layer (models, ORM)	1.11	Normal	wontfix	queryset iterator map	Sergey Fedoseev	Unreviewed	0	0	0	0	0	0
