python - 根据字典键生成django queryset

标签 python django dictionary django-queryset

我有一个像这样的字典:

{
    'key1' : val1,
    'key2' : val2
}

我需要一个像这样的查询集

Q(key1__icontains = val1) | Q(key2__icontains = val2)

谢谢

最佳答案

reduce(operator.or_, Q(**{key + '__icontains': val}) for (key, val) in D.iteritems())

关于python - 根据字典键生成django queryset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5350846/

相关文章:

json - reduce 函数正在打印一个空字典 [ :]

python - 如何将数据框转换为嵌套字典?

python - SQLAlchemy 类型对象 'Date' 没有属性 '_set_parent_with_dispatch'

python - django-pyodbc-azure SQL Server 参数限制

python - 在 req.path 中使用 django 的反向函数

c# - 制定更好的方法来将多个列表/字典包装在类中并循环键和值

python - auth.User.groups : (fields. E304) 'User.groups' 的反向访问器与 'UserManage.groups' 的反向访问器冲突

python - 在 django 中向租户管理员隐藏公共(public)模型

django - 用户组和权限

python - 在 python 中是否有类似 updated 的东西来更新 sorted 是要排序的?