在两个日期字段之间按年份按年份的过滤器Queryset
我的模型中有一个 start_date 和 end_date 字段。用户应能够通过选择年份值来过滤记录,以便如果包含所选年份,则应显示跨越多年的记录。
例如: 选择的年份: 2019
start_date | end_date |
---|---|
2017-03-12 | 2021-09-03 |
2019-12-12 | 2020-06-05 |
我可以通过原始 SQL 执行此查询,如下所示:
SELECT * FROM `orders` WHERE '2019' BETWEEN YEAR(`start_date`) AND YEAR(`end_date`);
How can I do this using Django ORM and避免原始 SQL 查询?因为我已经在多个过滤器中使用了 ORM,只留下了这一点。
I have a start_date and an end_date fields in my model. The user shall be able to filter records by selecting a year value, so that records that span over multiple years should be shown if the year selected is included.
For example:
Selected year: 2019
start_date | end_date |
---|---|
2017-03-12 | 2021-09-03 |
2019-12-12 | 2020-06-05 |
I can do this query by raw SQL like that:
SELECT * FROM `orders` WHERE '2019' BETWEEN YEAR(`start_date`) AND YEAR(`end_date`);
How can I do this using Django ORM and avoid raw SQL queries? Because I am already using the ORM in multiple filters and only remaining this bit.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试以下操作:
Try this: