weird lookup_type problem

104 views
Skip to first unread message

Bob Haugen

unread,
Mar 18, 2012, 7:03:31 PM3/18/12
to django...@googlegroups.com
Trying to understand how django_filters works, I played around with one of the test examples like this:

class BookFilter(django_filters.FilterSet):
    title = django_filters.CharFilter(lookup_type='contains')
    price = django_filters.NumberFilter(lookup_type='lte')
    average_rating = django_filters.NumberFilter(lookup_type='gte')

    class Meta:
        model = Book
        fields = ['title', 'price', 'average_rating']

So Filter.__init__ goes through the BookFilter twice for each of these fields.

The first time, the name is None and the lookup_type is as-specified (e.g. 'contains', 'lte', etc.).

The second time the name is the field name as listed in Meta.fields and the lookup_type is the default, 'exact'.  The second time is what counts and is used.

I tried a bunch of variations, putting the name in the filter def, putting the lookup_types in __init__, nothing worked.  The lookup_types all ended up being 'exact'.

What am I doing wrong?

Thanks,
Bob Haugen

Steve

unread,
Mar 19, 2012, 10:44:16 AM3/19/12
to django...@googlegroups.com
Remove fields=[...], as it doesnt' behave the same as in forms (falling back to an ordering)

-Steve

Bob Haugen

unread,
Mar 19, 2012, 11:12:58 AM3/19/12
to django...@googlegroups.com
On Mon, Mar 19, 2012 at 9:44 AM, Steve <sub...@gmail.com> wrote:
> Remove fields=[...], as it doesnt' behave the same as in forms (falling back
> to an ordering)

Thanks. But fields might be innocent. I will remove them, because
they are redundant in FilterSets, but the automatic filters based on
the fields list do get replaced by the declared filters.

The solution to the problem was adding a filter_class argument to the
url pattern. Otherwise, the generic view creates a new filterset from
the model, which of course gets the default 'exact' lookup_types.

Or at least that worked for me, without removing fields.

Reply all
Reply to author
Forward
0 new messages