Implementing a User Search Feature

532 views
Skip to first unread message

raj

unread,
Jun 30, 2011, 2:23:01 PM6/30/11
to Django users
Tried googling but couldn't come up with much.
How do I implement a User Search feature? Where an individual can type
in the first and/or last name of user and come up with results? I saw
some things about how to search through a site, but I think this is a
little different.
Thank you.

Shawn Milochik

unread,
Jun 30, 2011, 2:25:07 PM6/30/11
to django...@googlegroups.com
What search terms did you use in Google?

Stuart MacKay

unread,
Jun 30, 2011, 3:02:33 PM6/30/11
to django...@googlegroups.com
The canonical answer for search in django is probably solr,
http://lucene.apache.org/solr/ This probably covers all your search needs.

haystack, http://haystacksearch.org/ should provide an easy to use API

Stuart Mackay
Lisbon, Portugal

bruno desthuilliers

unread,
Jun 30, 2011, 4:27:35 PM6/30/11
to Django users
On Jun 30, 8:23 pm, raj <nano.ri...@gmail.com> wrote:
> Tried googling but couldn't come up with much.
> How do I implement a User Search feature?

https://docs.djangoproject.com/en/1.3/ref/models/querysets/#filter

Anything else ?

bruno desthuilliers

unread,
Jun 30, 2011, 4:30:33 PM6/30/11
to Django users
On Jun 30, 9:02 pm, Stuart MacKay <smac...@flagstonesoftware.com>
wrote:
> The canonical answer for search in django is probably solr,http://lucene.apache.org/solr/This probably covers all your search needs.
>
> haystack,http://haystacksearch.org/should provide an easy to use API
>

Yes, I think solr is really what the OP needs here. Or he could try a
custom rewrite of Google search and set up a server farm <g>

(did anyone say "overkill" ?)

Stuart MacKay

unread,
Jun 30, 2011, 5:45:08 PM6/30/11
to django...@googlegroups.com
bruno,

The OP specifically asked about searching through a site. While there
was not enough information in the question to determine the scope I
think it was reasonable to conclude that since raj was unable to easily
find the answer using google that he was probably not asking about
something that could easily be found in the django docs.

Stuart MacKay
Lisbon, Portugal

Doug Ballance

unread,
Jun 30, 2011, 11:25:24 PM6/30/11
to Django users
If you just want a simple user search (which seems to be what you are
asking for), it really depends on your models - but a basic approach I
usually use is to define a django form to use for the search, and
instead of defining a save method, make a method that returns a
queryset. As an off the top of my head example for a django auth user
(untested, I might have screwed something up):

class UserSearchForm(forms.Form):
first_name__istartswith=forms.CharField(label="First name",
required=False)
last_name__istartswith=forms.CharField(label="Last name",
required=False)

def queryset(self):
from django.contrib.auth.models import User
filters={}
for k,v in self.cleaned_data.items():
val=v.strip()
if val: filters[k]=val
return User.objects.filter(**filters)

Then use it in your view like any other form:

def user_search(request):
usf=UserSearchForm(request.GET or
request.POST,prefix='usersearch')
users=usf.queryset()
return render_to_response('sometemplate.html',{'users':users})

Now if you really do want to do text indexing and have full text
search capabilities then you should look at the other suggestions.
For many things those are either overkill or too generic.

raj

unread,
Jul 2, 2011, 12:53:42 PM7/2/11
to Django users
Thanks, when I get back to my production comp, ill give this a try.
Did know about this queryset thing. Thanks again.
Just a quick question, the **filters thing u placed, i would put
something like first_name last_name or something right?

Doug Ballance

unread,
Jul 2, 2011, 5:44:17 PM7/2/11
to Django users
**filters is python for expand the dict 'filters' into keyword
arguments.

ie
filters={'foo':1}
model.objects.filter(**filters)

is the same as
model.objects.filter(foo=1)

The filters variable is just temporary dictionary for building the
keyword arguments from the form data, electing to omit any empty
values (no point for a search), and then expanding that temp
dictionary into the keyword search arguments needed for the filter()
method. The names on the form correspond to the lookup names you'd
use for the query.

If you use that form to search for first name 'raj', the filters
dictionary should end up looking like:

{'first_name__istartswith':'raj'}
which would expand (**) to the equivalent of
User.objects.filter(first_name__istartswith='raj')

Andre Terra

unread,
Jul 2, 2011, 10:45:18 PM7/2/11
to django...@googlegroups.com
You can use Alex Gaynor's great app called django-filter available at

https://github.com/alex/django-filter

It's the best app for the job, hands down.


Sincerely,
André Terra

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

--
Sent from my mobile device

Reply all
Reply to author
Forward
0 new messages