Wildcard searching in Django
How can we do a wildcard searching in Django. If i am filtering username from a list in database, how is it possible to display the filtered data with those exact usernames or part of it.?
def filter(request):
val3=''
if request.GET.has_key('choices'):
val2=request.GE开发者_JAVA百科T.get('choices')
if request.GET.has_key('textField'):
val3=request.GET.get('textField')
if request.POST:
val2=request.POST.get('choices')
val3=request.POST.get('textField')
if val2=='Designation':
newData = EmployeeDetails.objects.filter(designation=val3)
flag=True
elif val2=='Name':
newData = EmployeeDetails.objects.filter(userName=val3)
flag=True
elif val2=='EmployeeID':
newData = EmployeeDetails.objects.filter(employeeID=val3)
flag=True
elif val2=='Project':
newData = EmployeeDetails.objects.filter(project=val3)
flag=True
elif val2=='DateOfJoin':
newData = EmployeeDetails.objects.filter(dateOfJoin=val3)
flag=True
else:
return HttpResponseRedirect('/employeeList/')
This is my function for filtering. Now its filtering with exact words. I want to display the userNames even if part of it is given for filtering. Please help me to solve this as i am new with Django
You can use contains
query e.g.
Entry.objects.get(headline__contains='Lennon')
See http://docs.djangoproject.com/en/dev/ref/models/querysets/#contains
There are other options too like startswith, endswith, and you can do even regex search on most databases.
Try using a less strict filter, like __contains:
elif val2=='Name':
newData = EmployeeDetails.objects.filter(userName__contains=val3)
flag=True
Docs: http://docs.djangoproject.com/en/dev/ref/models/querysets/#contains
Probably field lookups will help you here. It lets to filter by beginning of the word, containing word and so on.
精彩评论