Adding an 'or' to filter

13 views
Skip to first unread message

Clive Bruton

unread,
Jul 6, 2020, 12:03:34 PM7/6/20
to django...@googlegroups.com
I am working into someone else's solution, with this code in forms.py:

***************

class SearchForm(forms.Form):
area = forms.ModelChoiceField(label=_('Area'),
queryset=Area.objects.all(), required=False)
group = forms.ModelChoiceField(label=_('Group'),
queryset=Group.objects.all(), required=False)
q = forms.CharField(required=False, label=_('Query'),)

def filter_by(self):
# TODO search using more than one field
# TODO split query string and make seaprate search by words
filters = {}
if self.cleaned_data['group']:
filters['group'] = self.cleaned_data['group']

if self.cleaned_data['area']:
filters['area'] = self.cleaned_data['area']

filters['description__icontains'] = self.cleaned_data['q']

return filters

***************

I understand that 'filters' is a dictionary.

However, what I would like to do is have the final input ('q') be an
'or' search on two fields, ie:

filter(Q(description__icontains = self.cleaned_data['q']) | Q
(title__icontains = self.cleaned_data['q'])]

Not sure how to implement that in this dictionary style.

Thanks


-- Clive
Reply all
Reply to author
Forward
0 new messages