美文网首页
django 按时间筛选,今年,去年,上月

django 按时间筛选,今年,去年,上月

作者: 青穗黄 | 来源:发表于2018-03-09 12:03 被阅读213次

today = datetime.datetime.now()
1 Current year

Order.objects.filter(created_at__year=today.year)
2 Current month

Order.objects.filter(created_at__year=today.year, created_at__month=today.month)
3 Last month

last_month = today.month - 1 if today.month>1 else 12
last_month_year = today.year if today.month > last_month else today.year - 1

Order.objects.filter(created_at__year=last_month_year, created_at__month=last_month)
4 Last year

last_year = today.year - 1
Order.objects.filter(created_at__year=last_year)
5 Single Query

As last year + current year includes last month and current month, and all orders>= last_year includes current year, the query is super simple:

Order.objects.filter(created_at__year__gte=last_year)

from:[https://stackoverflow.com/questions/28101480/how-can-i-query-for-objects-in-current-year-current-month-in-django]

相关文章

  • django 按时间筛选,今年,去年,上月

    today = datetime.datetime.now()1 Current year Order.objec...

  • django 时间筛选

    两种方法:1.orm查询的方法 2.sql查询方法 这里的format_alerts_list是格式化sql输出

  • 统计学方法中的一些计算公式

    同比 = 用今年减去年再除以去年;(本期数-同期数)÷同期数×100% 。 环比 = 用本月减上月再除以上月;(本...

  • 学习小组D6-高岭之猹

    一、dplyr几个基本函数 1.select(),按列筛选 按列号筛选 按列名筛选 2.filter()筛选行 3...

  • 去年,今年

    去年的今天我在深圳,加不完的班,太阳没出来,就出门了,总是迎着月亮回到家,今年的今天我还在努力加班,只是没那么晚了...

  • 去年——今年💪💪

    去年这时,正紧张的备考,祈祷好运的眷顾。亦或是小紧张,亦或是平常心吧,总之我面临高考时并没有那么那么强烈的紧张感。...

  • 去年 今年

    去年的今天 我刚刚失恋 泪水模糊了双眼 看不清路人的脸 去年的今天 我翘了一节课 为了提前回家一天 那时候 我还喜...

  • 去年今年

    时间过得好快,转眼一年时间消逝……记得去年的今天,还在为新店忙碌着,而现在、却在家带着刚满三个月的小女儿。过去的一...

  • 去年,今年

    无意中翻到图库里去年去北京时的照片,那是六月下旬,女儿刚刚中考完,在燕子的建议下重新下载了微信,有些功能还不会用,...

  • 去年,今年。

    新的一年,新的征程,与悠闲的2016年说再见,用全新的方式开启2017年。 回首2016年,一个字形容“懒!”“懒...

网友评论

      本文标题:django 按时间筛选,今年,去年,上月

      本文链接:https://www.haomeiwen.com/subject/yxypfftx.html