Thursday, June 30, 2011

Re: Implementing a User Search Feature

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.

--
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