Monday, May 2, 2011

Re: django searching

On Mon, 2011-05-02 at 21:40 -0700, pankaj sharma wrote:
> hello all,
> i am trying to have an advanced search..
> i have college list where there are many tables in college like
> college.name, collge.city college.state etc.
>
> so i have taken two strings from the user like Q1 and Q2.
> now i want to show all the colleges which have Q1 string in name and
> Q2 string in city..
> basically i want to add "and" & "or" statement in this statement :
>
>
> college_list = College.objects.filter(name__icontains=Q1)
>
>
> so i want to add one another filter like (city__icontains=Q2)
>

You didn't really specified do you want AND query or OR query.

And query is simple:

college_list = College.objects.filter(name__icontains=Q1,
city__icontains=Q2)

Or query is sligthly different but not much, you need to use Q-objects
for that (they're very powerful beings, you should read about them in
the docs.)

college_list = College.objects.filter((Q(name__icontains=Q1) |
Q(city__icontains=Q2))

Hope that helps.

--

Jani Tiainen


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment