Столкнулся с проблемой. вот вьюха:
@login_required
def use_filter(request):
if not request.is_ajax():
if 'filter_id' in request.GET:
flt = Filter.objects.only('content_type').get(pk = request.GET['filter_id'])
model = ContentType.model_class(flt.content_type)
kwargs = {}
for c in flt.conditions.all():
...
qs = model.objects.filter(**kwargs)
response = {}
for i,q in enumerate(qs):
field_list = {}
for f in q._meta.get_all_field_names():
z = q._meta.get_field_by_name(f)
obj = z[0]
model = z[1]
direct = z[2]
m2m = z[3]
if not direct:
continue
if m2m:
l = {}
for x,m in enumerate(z[0].value_from_object(q)):#проблема тут!!!
l.update({ix:m})
field_list.update({f:l})
else:
field_list.update({f:z[0].value_to_string(q)})
...
Собственно проблема в чем, get_field_by_name()
возвращает (field_object, model, direct, m2m)
.
вот описалово:
get_field_by_name
Returns the (field_object, model, direct, m2m), where field_object is the Field instance for the given name, model is the model containing this field (None for local fields), direct is True if the field exists on this model, and m2m is True for many-to-many relations. When ‘direct’ is False, ‘field_object’ is the corresponding RelatedObject for this field (since the field doesn’t have an instance associated with it). Uses a cache internally, so after the first access, this is very fast.
суть понятна. Проблема возникает тогда, когда я пытаюсь получить значения полей в for.
Я определяю м2м
-ли это и не related
-ли и если все ок, то говорю ему пройтись по всем записям и вернуть мне через value_from_object()
собственно само значение.
И все казалось бы гладко, но у меня в приложении есть generic
связи. И когда цикл натыкается на нее, то возвращает не СПИСОК, а... GenericRelatedObjectManager
. При этом, direct == True
.
В итоге имеем
Traceback:
File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/auth/decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django_pimp_my_filter-0.1b-py2.6.egg/filter_manager/views.py" in use_filter
131. for x,m in enumerate(z[0].value_from_object(q)):
Exception Type: TypeError at /pimp-my-filter/use_filter/
Exception Value: 'GenericRelatedObjectManager' object is not iterable
ковыряюсь вот и пока в голову ничего не приходит.
ЗЫ: это часть django-pimp-my-filter