在Django Rest Framework中按用户名和显示名称搜索的查询集:

huangapple go评论69阅读模式
英文:

Queryset for search by username and display_name In Django Rest Framework

问题

  • 我试图通过他们的用户名和显示名称来搜索用户。

  • 我已经使用了这个开始匹配的方法。

search_obj = User.objects.exclude(user_uuid=token_user_uuid).filter(Q(display_name__istartswith=search_name)
                | Q(username__istartswith=search_name)
                ).order_by('-created_on')[:10]

我得到了与我想要的答案相同的答案,但问题是如果显示名称是William Welch,而我搜索Welch,它应该返回该用户,但它没有返回该用户。

情况

用户名123William
显示名称William Welch

如果搜索名称为12则匹配
如果搜索名称为23w则不匹配
如果搜索名称为Wil则匹配
如果搜索名称为Welc则匹配
如果搜索名称为elch则不匹配
英文:

I'm trying to search users by their usernames and display names.

  • I have used that for the starting match.
search_obj = User.objects.exclude(user_uuid=token_user_uuid).filter(Q(display_name__istartswith=search_name)
                | Q(username__istartswith=search_name)
                ).order_by('-created_on')[:10]

I get the answer same as I want but the problem is if the display name is William Welch and I search for Welch It should return that user also, but it does not return that user.

cases.

username: 123William
display name: William Welch

if search_name 12 then match
if search_name 23w then not match
if search_name Wil then match
if search_name Welc then match
if search_name elch then not match

答案1

得分: 1

你可以使用__icontains&nbsp;<sup>[Django文档]</sup>进行子字符串搜索:

from django.db.models import Q

search_obj = (
    User.objects.exclude(user_uuid=token_user_uuid)
    .filter(
        display_name<b>__icontains</b>=search_name,
        username<b>__icontains</b>=search_name,
        _connector=Q.OR,
    )
    .order_by('-created_on')[:10]
)

如果你想搜索以特定词开头的内容,可以使用**__iregex**查找&nbsp;<sup>[Django文档]</sup>来使用正则表达式:

import re

from django.db.models import Q

rgx = fr'\y{re.escape(search_name)}'

search_obj = (
    User.objects.exclude(user_uuid=token_user_uuid)
    .filter(
        display_name<b>__iregex=rgx</b>,
        username<b>__iregex=rgs</b>,
        _connector=Q.OR,
    )
    .order_by('-created_on')[:10]
)
英文:

You can search with __icontains&nbsp;<sup>[Django-doc]</sup> to look for a substring instead:

<pre><code>from django.db.models import Q

search_obj = (
User.objects.exclude(user_uuid=token_user_uuid)
.filter(
display_name<b>__icontains</b>=search_name,
username<b>__icontains</b>=search_name,
_connector=Q.OR,
)
.order_by('-created_on')[:10]
)</code></pre>

If you want to search for starting words, you can work with a regex with the __iregex lookup&nbsp;<sup>[Django-doc]</sup>:

<pre><code>import re

from django.db.models import Q

rgx = fr'\y{re.escape(search_name)}'

search_obj = (
User.objects.exclude(user_uuid=token_user_uuid)
.filter(
display_name<b>__iregex=rgx</b>,
username<b>__iregex=rgs</b>,
_connector=Q.OR,
)
.order_by('-created_on')[:10]
)</code></pre>

huangapple
  • 本文由 发表于 2023年1月9日 18:41:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75056087.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定