python - 如何在 Django 中进行此查询?
问题描述
我有这些字段(test_type, status, begin_time, and_time
)。该status
字段具有三个状态(0
或1
)2
。我想做一个查询集,其中:
(test_type = 'difficult') and
(if status=1 and begin_time + 2x(end_time - begin_time) < timezone.now()) or
(if status=2 and end_time < timezone.now())
然后选择这些字段。如果状态 state=0 我不想选择该字段。如何使用 Django ORM 制作这个查询集?
解决方案
您将不得不在 Django 中使用 Q 对象,
Model.objects.filter((Q(status=1) & Q(begin_time < computed_value_here)) | (Q(status=2) & Q(end_time < computed_value_here)), test_type='difficult')
推荐阅读
- android - 在 rxjava2 中的异步请求完成之前调用订阅者 onNext
- php - 检查 PHP 中的路径遍历并允许使用未损坏的非现有路径
- java - 无法将 TextView 转换为 java.io.Serializable
- asp.net - Visual Studio 2017 Angular 6 显示“无法获取 /”,仅限首次尝试
- loops - 使用 Jinja2 模板循环访问主机
- ios - 无法更改 UITableViewCell 内 UIImageView 的宽度和高度 - swift
- r - r - 折线图未连接(财务数据)
- eloquent - eloquent 5.6 模型创建功能未找到
- nginx - 多个网站的Nginx反向代理和root问题
- javascript - 匹配标签内的所有换行符