Multiple icontains using OR - Is it possible?

1,085 views
Skip to first unread message

tm

unread,
Dec 15, 2009, 10:32:07 PM12/15/09
to Django users
Hello Django Users,

I'm trying to use icontains to check 2 fields in the same table for a
keyword entered into a search.

ex I've tried:

queryset = MyModel.objects.filter(summary__icontains=q).filter
(title__icontains=q)

and

queryset = MyModel.objects.filter(summary__icontains=q,
title__icontains=q)

Neither work, although judging by the docs the second one shouldn't in
this case. Has anyone successfully searched a MySQL DB using
icontains on 2 filelds in the same model? Any help greatly
appreciated :)

T

HARRY POTTRER

unread,
Dec 15, 2009, 11:27:35 PM12/15/09
to Django users
you're going to need the Q object, it seems.

from django.db.models import Q
MyModel.objects.filter(Q(summary__icontains=q) | Q
(title__icontains=q))

tm

unread,
Jan 21, 2010, 9:02:39 PM1/21/10
to Django users
This worked! Thank you
Reply all
Reply to author
Forward
0 new messages