首页 > 技术文章 > 将年月日按年月、周、日...分类

chijintao 2021-06-03 09:53 原文

from django.db.models.functions import TruncMonth

from django.db.models import Count

 

Sales.objects

    .annotate(month=TruncMonth('timestamp'))  # Truncate to month and add to select list

    .values('month')                          # Group By month

    .annotate(c=Count('id'))                  # Select the count of the grouping

    .values('month', 'c')                     # (might be redundant, haven't tested) select month and count 

 

推荐阅读