Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在django中查询当前年份、当前月份的对象_Python_Django_Datetime - Fatal编程技术网

Python 如何在django中查询当前年份、当前月份的对象

Python 如何在django中查询当前年份、当前月份的对象,python,django,datetime,Python,Django,Datetime,我需要找到在中创建的所有对象 1. current year 2. current month 3. last month 4. last year 我是这样想的 this_year = datetime.now().year last_year = datetime.now().year -1 this_month = datetime.now().month last month = (datetime.today() - timedelta(days=30

我需要找到在中创建的所有对象

1. current year
2. current month
3. last month
4. last year
我是这样想的

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

Order.objects.filter(在本月创建)

问题是

  • 我想要的最后一个月是日历月,而不是30天前
  • 我不确定在本月创建的月份是否与当前月份或上一年的同一月份匹配
  • 是否可以在单个查询中获取所有计数

  • 如果您想在单独的查询中使用它,可以这样做

    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)
    

    注意:在我的示例中,我将'06'表示为六月,但您可以更改它

    我认为,如果不进行一些重要的修改或注释,就无法只匹配日期字段的“月”或“年”部分。最有可能的是,最简单的解决方案是定义所需范围的开始和结束,并根据该范围进行搜索。这可能需要一些工作

    例如,最后一个日历月是:

    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和LTE是“大于或等于”和“小于或等于”。同样值得注意的是,我们使用timedelta来计算本月1日前一天是什么,而不是通过所有不同的案例来判断前一个月是28天、29天、30天还是31天

    today = datetime.datetime.now()
    
    1本年度

    Order.objects.filter(created_at__year=today.year)
    
    2本月

    Order.objects.filter(created_at__year=today.year, created_at__month=today.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 = today.year - 1
    Order.objects.filter(created_at__year=last_year)
    
    5单个查询

    由于去年+本年包括上月和本月,所有订单>=上一年包括本年,所以查询非常简单:

    Order.objects.filter(created_at__year__gte=last_year)
    

    我希望所有这些过滤器都是分开的,我的意思是它们不是ORs。这将是4个不同的数字这很完美,单次查询我的意思是说使用annotate在single查询中获得4个不同的计数,但这是可以的。我认为如果您在
    settings.py
    中打开了时区,您可能希望使用Django的时区而不是datetime
    from django.utils导入时区now=timezone.now()