django-polymorphic icon indicating copy to clipboard operation
django-polymorphic copied to clipboard

Query translation - tuple check

Open till89 opened this issue 7 years ago • 2 comments

Hello everybody,

I stumbled upon the following issue while implementing advanced filtering in the admin views (using advanced-filters): Polymorphic models can't be filtered, because the Q object passed from django-advanced-filters is not interpreted correctly (or the Q passed from django-advanced-filters does not conform to standards):

AttributeError
'list' object has no attribute 'children'
/home/till/.virtualenvs/venv/local/lib/python2.7/site-packages/polymorphic/query_translate.py  in tree_node_correct_field_specs, line 59

The error occurs in query_translate.py:59 when trying to iterate over what are presumed to be nested Q's - node in my problematic context is a list, but the query translation assumes it to be another Q object. This "recursion" is caused by this (excerpt below):

if type(child) == tuple:
    # a kwarg like tuple
else:
    # child is another Q object

In my case, the Q is neither a tuple nor a Q, and thus the control flow fails.

I am not sure whether this is something that should be fixed in django-advanced-filters or here, in django-polymorphic (or actually any where at all), but I found a very easy fix for my problem by simply changing the check for a tuple to include a list:

if isinstance(child, (tuple, list)):
     # a kwarg like tuple
else:
    # child is another Q object

Because I am not sure whether that is on purpose or not, I have not yet created an official pull request (the full code is here).

till89 avatar Nov 23 '17 15:11 till89