Django 中的通配符搜索

发布于 2024-10-20 13:50:42 字数 1138 浏览 2 评论 0原文

我们如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

森林很绿却致人迷途 2024-10-27 13:50:43

尝试使用不太严格的过滤器,例如 __contains:

elif val2=='Name':
        newData = EmployeeDetails.objects.filter(userName__contains=val3)
        flag=True 

Docs: http:// docs.djangoproject.com/en/dev/ref/models/querysets/#contains

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

寄人书 2024-10-27 13:50:43

也许 字段查找 会在这里帮助你。它可以按单词开头、包含单词等进行过滤。

Probably field lookups will help you here. It lets to filter by beginning of the word, containing word and so on.

宛菡 2024-10-27 13:50:42

您可以使用 contains 查询,例如

Entry.objects.get(headline__contains='Lennon')

参见 http: //docs.djangoproject.com/en/dev/ref/models/querysets/#contains

还有其他选项,例如 startswith, 结尾,你甚至可以做 regex 在大多数数据库上搜索。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文