给出以下模型:
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选项使搜索不敏感。在
我相信(如果我错了请纠正我)djangtoolbox 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?
相关问题 更多 >
编程相关推荐