jazzband / django-polymorphic

Improved Django model inheritance with automatic downcasting
https://django-polymorphic.readthedocs.io
Other
1.65k stars 280 forks source link

Query translation - tuple check #323

Open till89 opened 6 years ago

till89 commented 6 years ago

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).

vdboor commented 6 years ago

hi!

could you provide an small bit of example code, so we can reproduce this issue? Thanks!