【问题标题】:How to search CharField in Django?如何在 Django 中搜索 CharField?
【发布时间】:2016-01-12 01:57:52
【问题描述】:

我想做一个搜索功能:在我搜索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.

我该怎么做?

【问题讨论】:

    标签: python mysql django python-2.7 orm


    【解决方案1】:

    我自己找答案!

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

    Field lookups

    iexact

    如果我使用iexact

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

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

    以及 SQL 等价物:

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

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

    【讨论】:

      猜你喜欢
      • 2014-08-01
      • 2017-07-02
      • 2021-04-15
      • 2020-12-02
      • 1970-01-01
      • 2012-08-29
      • 1970-01-01
      • 2021-09-13
      • 1970-01-01
      相关资源
      最近更新 更多