简体   繁体   English

如何在当前年份,django中查询当前年份的对象

[英]How can i query for objects in current year , current month in django

I need to find total objects created in 我需要找到创建的总对象

1. current year
2. current month
3. last month
4. last year

I am thinking like this 我在想这个

    this_year = datetime.now().year
    last_year = datetime.now().year -1
    this_month = datetime.now().month
    last month = (datetime.today() - timedelta(days=30)).month

Use like 用得像

Order.objects.filter(created_at__month=this_month)

The problem is 问题是

  1. last_month i want is calendar month not 30 days back last_month我想要的是日历月而不是30天
  2. i am not sure whether created_at__month=this_month will match current month or same month in previous year 我不确定created_at__month = this_month是否与当前月份或上一年的同月相匹配
  3. is it possible to get all counts in single query 是否有可能在单个查询中获得所有计数
today = datetime.datetime.now()

1 Current year 1本年度

Order.objects.filter(created_at__year=today.year)

2 Current month 2本月

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

3 Last month 3上个月

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 4去年

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

5 Single Query 5单个查询

As last year + current year includes last month and current month, and all orders>= last_year includes current year, the query is super simple: 由于去年+当前年度包括上个月和当月,并且所有订单> = last_year包括当前年份,查询非常简单:

Order.objects.filter(created_at__year__gte=last_year)

If you want it in separate queries, do something like that. 如果您想在单独的查询中进行,请执行类似的操作。

from_this_year = Order.objects.filter(created_at__year=this_year)
from_last_year = Order.objects.filter(created_at__year=last_year)
from_june = Order.objects.filter(created_at__month='06',created_at__year=this_year)
from_this_month = Order.objects.filter(created_at__month=this_month,created_at__year=this.year)

note: in my example, I put '06' that is June, but you can change it. 注意:在我的例子中,我把6月的'06',但你可以改变它。

I don't think you'll be able to just match the "month" or "year" part of a date field without some significant fiddling or annotating. 我认为你不能只匹配一个日期字段的“月”或“年”部分而没有一些重要的小提琴或注释。 Most likely, your simplest solution is to define the start and end of the range you want and search against that. 最有可能的是,您最简单的解决方案是定义所需范围的开始和结束,然后对其进行搜索。 And that might involve a little bit of work. 这可能涉及一些工作。

For example, last calendar month would be: 例如,上个日历月将是:

today = datetime.now()
if today.month == 1:
    last_month_start = datetime.date(today.year-1, 12, 1)
    last_month_end = datetime.date(today.year-1, 12, 31)
else:
    last_month_start = datetime.date(today.year, today.month -1, 1)
    last_month_end = datetime.date(today.year, today.month, 1) - datetime.timedelta(days=1)
Order.objects.filter(created_at__gte=last_month_start, created_at__lte=last_month_end)

GTE and LTE are "greater than or equal" and "less than or equal". GTE和LTE“大于或等于”和“小于或等于”。 Also worth noting, we use timedelta to figure out what the day before the first of this month is rather than go through all the different cases of whether the previous month had 28, 29, 30 or 31 days. 另外值得注意的是,我们使用timedelta来确定本月第一天的前一天是什么,而不是经历上个月是否有28天,29天,30天或31天的所有不同情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM