【问题标题】:Search string in database column django query在数据库列 django 查询中搜索字符串
【发布时间】:2018-02-21 15:56:33
【问题描述】:

我在 db 中有一个字段,其中包含类似 "Food,Financial,Health" 的字符串。我需要在该字段中搜索以“Fi”开头的字符串。我可以使用 contains 搜索字符串,但我需要在 django 查询中搜索startswith。任何人都建议我这样做。

【问题讨论】:

标签: python django python-2.7


【解决方案1】:

您可以在 django 查询中使用startswith ..

SomeModel.objects.filter(stringField__startswith='What')

【讨论】:

    【解决方案2】:

    您可以使用 django __startswith 过滤器属性。

    YourModelClass.objects.filter(fieldname__startswith='Fi')
    

    在此处查看文档: https://docs.djangoproject.com/en/1.11/ref/models/querysets/#startswith

    【讨论】:

      【解决方案3】:
      Modelname.objects.filter(attributename__istartswith='Fi')
      

      更多详情请参考https://docs.djangoproject.com/en/1.11/ref/models/querysets/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-11-23
        • 1970-01-01
        • 2015-05-27
        • 2021-01-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多