Python Django集团(按日期和类别计数)

Python Django集团(按日期和类别计数),python,mysql,django,Python,Mysql,Django,示例模型 class Category(models.Model) name = models.CharField() class CategoryItem(models.Model): category = models.ForeignKey(Category) name = models.CharField() quantity = models.IntField() at = models.DateTimeField() Categories:

示例模型

class Category(models.Model)
    name = models.CharField()

class CategoryItem(models.Model):
    category = models.ForeignKey(Category)
    name = models.CharField()
    quantity = models.IntField()
    at = models.DateTimeField()


Categories:
    1, Category #1
    2, Category #2
    3, Category #3

Items:
    ID,NAME,CATEGORY_ID,QUANTITY,AT
    1,Item #1,1,100,2017.1.1
    2,Item #2,1,200,2017.1.3
    3,Item #3,1,300,2017.1.3
    4,Item #4,2,400,2017.1.10
    5,Item #5,3,500,2017.1.10
我怎样才能按日标注并计算每个类别的“数量”

像这样

 2017.1.1
    - Category #1
        - Quantity: 100

2017.1.3
    - Category #1
        - Quantity: 500

2017.1.10
    - Category #2
        - Quantity: 400
    - Category #3
        - Quantity: 500

如果我理解您的问题,您需要一个带有GROUPBY和count(以及内部联接)的Django查询

试着这样做:

CategoryItem.objects.filter(CategoryItem__Category__isnull=False).values('at,name').annotate(total=Count('quantity')).order_by('at')

欢迎来到堆栈溢出!请拿着这本书,四处看看,特别是通读一下。你上面问的问题一点也不清楚,你能编辑你的问题来澄清吗?也许你希望这里有免费的代码编写服务?