英文:
Search with or without space in django filter
问题
我有一个按名称搜索的功能,如果搜索匹配名称,应该返回一个人的名称。我需要显示有或没有空格的结果。例如:'A B C'应该显示结果A B Chacko,AB Chacko,AB Chaks等。类似地,搜索词'ABC'也需要列出上述结果。
try:
term = request.GET['term']
if term:
queryset_primary = PrimaryUserSerializer(UserTable.objects.filter(Q(name__icontains=term) | Q(occupation__icontains=term)).order_by('name'), many=True, context=context).data
else:
pass
except:
pass
模型只有'name'字段,没有名字和姓氏。
英文:
I have a search by name function, which should return the name of one person if the search matches the name.I need to show results with or without space between them. Eg: ‘A B C’
should show results A B Chacko, AB Chacko, AB Chaks etc. similarly, the search
The term ‘ABC’ needs also list the above results.
try:
term = request.GET['term']
if term:
queryset_primary = PrimaryUserSerializer(UserTable.objects.filter(Q(name__icontains =term)|Q(occupation__icontains=term)).order_by('name'), many=True, context=context).data
else:
pass
except:
pass
The models only have the 'name ' field not have first name, last name
答案1
得分: 1
请尝试以下代码,但这对性能很不好。
try:
term = request.GET['term']
if term:
queryset_primary = PrimaryUserSerializer(UserTable.objects.filter(Q(name__icontains=term) | Q(name__istartswith=term) | Q(name__iexact=term) | Q(name__iendswith=term) | Q(name__startswith=trm) | Q(occupation__icontains=term)).order_by('name'), many=True, context=context).data
else:
pass
except:
pass
英文:
Try this code but this is very bad for performance.
try:
term = request.GET['term']
if term:
queryset_primary = PrimaryUserSerializer(UserTable.objects.filter(Q(name__icontains =term) | Q(name__istartswith=term) | Q(name__iexact=term) | Q(name__iendswith=term) | Q(name__startswith=trm)| Q(occupation__icontains=term)).order_by('name'), many=True, context=context).data
else:
pass
except:
pass
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论