python - Django group by date and count per category -
example models
class category(models.model) name = models.charfield() class categoryitem(models.model): category = models.foreignkey(category) name = models.charfield() quantity = models.intfield() @ = 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 how can annotate day , count "quantity" per category
like this
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
if understand question, want django query group , count (and inner join).
try this:
categoryitem.objects.filter(categoryitem__category__isnull=false).values('at,name').annotate(total=count('quantity')).order_by('at')
Comments
Post a Comment