How to have filter return ORed results of multiple fields

119 views
Skip to first unread message

Mitch

unread,
Aug 29, 2011, 6:36:18 PM8/29/11
to django...@googlegroups.com
As part of a model, I have a last_name and first_name field. For example:

class Member(models.Model):
    last_name = ...
    first_name = ...

I'd like to have an input text box in a template for member name for which the filter will return results if the text is found in either last_name or first_name. Is this possible? How would I do it?

Steve

unread,
Aug 29, 2011, 6:40:06 PM8/29/11
to django...@googlegroups.com
create your own filter called 'name' which conducts the search.

Mitch

unread,
Aug 29, 2011, 7:03:09 PM8/29/11
to django...@googlegroups.com
How is that incorporated into the django-filter infrastructure? Member is a model related to the model being filtered.  If I want just the last_name, I would have:

member__last_name = django_filters.filterset.CharFilter(lookup_type='icontains')

and in Meta:

fields = ['member__last_name']

I can put name in __init__ (if that's where it should go) to get the ORed queryset, but how does it get the argument from the input and then return the resulting queryset?

Steve

unread,
Aug 29, 2011, 7:05:51 PM8/29/11
to django...@googlegroups.com
You just specify your own filter object, subclassing Filter, its not connected to a model name but it conducts the same work. Check filters.py
Reply all
Reply to author
Forward
0 new messages