gpt4 book ai didi

python - 如何在 Django 中搜索 CharField?

转载 作者:行者123 更新时间:2023-11-29 11:56:31 24 4
gpt4 key购买 nike

我想做一个搜索功能:我使用GodX在用户管理中注册我的用户名,当我搜索godx作为用户名关键字后,我可以得到 GodX 对象。

目前我只能执行以下操作:

from django.contrib.auth.models import User

user = user.objects.get(username="godx")
# I hope I can get `GodX` object but the method is unavailable.

我该怎么办?

最佳答案

我自己找到答案!

根据 django 文档中的这些链接:

Field lookups

iexact

如果我使用iexact:

>>> Blog.objects.get(name__iexact="beatles blog")

它将匹配标题为“Beatles Blog”、“beatles blog”甚至“BeAtlES blOG”的博客。

以及 SQL 等效项:

SELECT ... WHERE name ILIKE 'beatles blog';

所以,我可以得到我所期望的。

关于python - 如何在 Django 中搜索 CharField?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33117542/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com