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

时间:2017-12-04 21:40:16

标签: python django django-queryset

鉴于以下模型:

bahia = Team()
bahia.name = "E.C. Bahia"
bahia.others = {"title": "Ninguém nos Vence em Vibração!!!"}
bahia.save()

vicetoria = Team()
vicetoria.name = "E.C. Vicetoria"
vicetoria.others = {"title": "Vice de tudo!"}
vicetoria.save()

以下代码:

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

我想在字段其他标题值中找到包含 vence ,(不区分大小写)字样的对象。< / p>

我尝试过类似的事情:

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

给了我以下错误:

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

我也试过了:

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

返回None,我知道至少有一个集合。

SOLUTION:

$categories

i 选项会使搜索不敏感。

1 个答案:

答案 0 :(得分:0)

我相信(如果我错了,请纠正我)djangotoolbox DictField不支持icontain。

要过滤Dict字段,您需要执行以下操作:

Team.objects.raw_query({'title': { $regex : /vence/i } })

查看此答案:How do I filter based on dict contents in a DictField on a model using the Django-MongoDB Engine?

此答案显示如何执行不区分大小写的mongodb查询:How do I make case-insensitive queries on Mongodb?