gpt4 book ai didi

django - 我们如何从单个 django orm 查询中获取多个值?

转载 作者:行者123 更新时间:2023-11-29 14:31:49 25 4
gpt4 key购买 nike

class Test(ModelBase):
team = models.IntegerField(null=True)



id created_at team
1 2018-02-14 10:33:46.307214+05:30 1
2 2018-02-24 10:41:40.307681+05:30 1
3 2018-03-14 12:00:02.748088+05:30 1
4 2018-03-12 18:05:33.598297+05:30 3
5 2018-03-12 18:04:00.694774+05:30 3
6 2018-04-14 11:24:48.554993+05:30 3
7 2018-04-14 11:42:27.442441+05:30 3
8 2018-05-11 15:53:21.528813+05:30 3
9 2018-05-08 15:49:58.684201+05:30 3

我们如何从单个查询中获得当月 & team = 3 计数,上个月 & team = 3 计数?

最佳答案

您可以使用 Q object允许使用 OR 条件执行查询:

current_month = datetime.datetime.now().month
first_day_of_current_month = datetime.datetime.now().replace(day=1)
last_month = (first_day_of_current_month - datetime.timedelta(days=1)).month

Test.objects.filter(Q(created_at__month=current_month, team=3)|Q(created_at__month=last_month, team=3))

关于django - 我们如何从单个 django orm 查询中获取多个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50637283/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com