简体   繁体   English

如何在Django中搜索CharField?

[英]How to search CharField in Django?

I want to do a search feature: I use GodX to register my username in user management after I search godx as username keyword, I can get the GodX object. 我想做一个搜索功能:搜索godx作为用户名关键字后,我使用GodX在用户管理中注册我的用户名,就可以得到GodX对象。

Currently I am limited to do the following: 目前,我只能执行以下操作:

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.

How do I do? 我该怎么办?

I find answer by myself! 我自己找到答案!

According to these links from django document: 根据django文件中的这些链接:

Field lookups 现场查询

iexact 精确

If I use iexact : 如果我使用iexact

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

It would match a Blog titled "Beatles Blog", "beatles blog", or even "BeAtlES blOG". 它会匹配名为“甲壳虫博客”,“甲壳虫博客”甚至“ BeAtlES blOG”的博客。

And the SQL equivalents: 和SQL等效项:

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

So, I can get I expect. 所以,我可以得到我的期望。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM