django - Django ORM 添加变量而不是 .filter 指令
问题描述
在我的项目中,我有一个带有 django ORM 查询的函数。我会将传递给函数的值列表传递给 .filter 部分:
def testFilter(filterlist):
#for example filterlist could be: thread='DEAD',id=11,t_type='SA'
myquery = t_threads.objects.filter(filterlist).select_related().order_by(lorder)[x:y]
但如果我,例如运行我的功能:
testFilter("id=1,thread='LIVE'")
我有一个错误:
ValueError:要解包的值太多(预期为 2)
每次运行该函数时,如何通过过滤器指令运行我的 ORM 查询?
非常感谢提前
解决方案
您应该kwargs
为此使用:
def testFilter(**kwargs):
myquery = t_threads.objects.filter(**kwargs)...
testFilter(id=1, thread='LIVE')
推荐阅读
- c# - 如何在不拦截游戏控件的情况下使用按钮?
- java - 从 @MappedSuperClass 更改为 @Inheritance 时,超级实体类的 Spring 存储库未获取所有子类实体类型
- magento - Magento 1:2020 年底停止支持 Flash
- powershell - PowerShell Az 模块:离线导出和导入
- c - C 分段错误 - 在 .dat 文件中保存结构
- python - Python selenium 等待浏览器在启动时加载
- android - 如何在 Android FirebaseMessagingService 中创建倒计时?
- javascript - 如何确定 Selenium 中 WebElement 的有效 CSS 属性?
- javascript - 你能解释一下下面提到的代码中的执行顺序吗?
- reactjs - React,ESLint:eslint-plugin-react-hooks 对对象中函数的依赖