Python 数据库列django查询中的搜索字符串

Python 数据库列django查询中的搜索字符串,python,django,python-2.7,Python,Django,Python 2.7,我在db中有一个字段,其中包含类似“Food、Financial、Health”的字符串。我需要搜索该字段中以“Fi”开头的字符串。我可以使用contains搜索字符串,但我需要在django查询中搜索startswith。有人建议我这样做。您可以在django查询中使用StartWith SomeModel.objects.filter(stringField__startswith='What') 您可以使用django\uuu startswith filter属性 YourModelC

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

您可以在django查询中使用StartWith

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

您可以使用django\uuu startswith filter属性

YourModelClass.objects.filter(fieldname__startswith='Fi')
请参见此处的文档:


有关更多详细信息,请参阅

您应该首先搜索,如果您搜索django查询,则这是第一个搜索结果可能重复的
Modelname.objects.filter(attributename__istartswith='Fi')