Django 中的通配符搜索
我们如何在 Django 中进行通配符搜索。如果我从数据库中的列表中过滤用户名,如何使用这些确切的用户名或部分用户名显示过滤后的数据?
def filter(request):
val3=''
if request.GET.has_key('choices'):
val2=request.GET.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/')
这是我的过滤功能。现在它用精确的单词进行过滤。我想显示用户名,即使其中一部分用于过滤。请帮我解决这个问题,因为我是 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.GET.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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用不太严格的过滤器,例如 __contains:
Docs: http:// docs.djangoproject.com/en/dev/ref/models/querysets/#contains
Try using a less strict filter, like __contains:
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.
您可以使用
contains
查询,例如参见 http: //docs.djangoproject.com/en/dev/ref/models/querysets/#contains
还有其他选项,例如 startswith, 结尾,你甚至可以做 regex 在大多数数据库上搜索。
You can use
contains
query e.g.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.