如何 在Django查询集的DictField键值中使用icontains?

2024-10-02 10:25:15 发布

您现在位置:Python中文网/ 问答频道 /正文

给出以下模型:

class Team(models.Model):
    name = models.CharField(max_length=50)
    others = DictField()

以及以下代码:

^{pr2}$

我想找到包含vence字段值others中包含单词vence的对象(不区分大小写)。在

我试过这样的方法:

teams = Team.objects.filter(others__title__icontains="vence")

这给了我以下错误:

FieldError: Join on field 'others' not permitted. Did you misspell 'title' for the lookup type?

我也试过了:

teams = Team.objects.filter(others__icontains={"title":"vence"})

它返回None,我知道结果至少有一个集合。在

解决方案:

teams = Team.objects.raw_query({"others.title": {"$regex" : "vence", "$options": "i"}})

i选项使搜索不敏感。在


Tags: name模型modelobjectstitlemodelsfiltermax

热门问题