私信  •  关注

Dhanushka Amarakoon

Dhanushka Amarakoon 最近回复了
11 月前
回复了 Dhanushka Amarakoon 创建的主题 » Django 1.11.20获取每个ISO周和年的行数

我想起来了,我忘了点菜了

from django.db.models.functions import ExtractWeek, ExtractYear
from django.db.models.aggregates import Count

count = myModel.objects.annotate(year=ExtractYear('created_at')) \
    .annotate(week=ExtractWeek('created_at')) \
    .values('year','week') \
    .annotate(count=Count('id')).order_by('count')

<QuerySet [{'year': 2019, 'week': 7, 'count': 8}]>