most amount of field of day django

Viewed 26

I want to list the most amounts of field of a model for each day for example this be my model:

class Topic(Model.models): 
    title = models.Charfield(max_lenth=40)
    total_responses = models.PositiveIntegerField()
    date = models.DateTimeField(add_now=True)

I have imported Max before in my views.py and this is my query set:

query = Topic.objects.values(
            'date','total_responses',
        ).order_by('-date').aggregate(Max('total_responses'))

I am sending this query to template but it returns all models! but I just need the max amount of each day model

1 Answers

If you are trying to get the row which contains the max value of total_responses field for the current day, your query should be something like this:

from django.utils.timezone import datetime #important if using timezones
today = datetime.today()
query = Topic.objects.values(
            'date','total_responses',
        ).filter(
            date__year=today.year, 
            date__month=today.month, 
            date__day=today.day,
        ).order_by('-total_responses').first()

This will give you only 1 row for each day with max total_responses

Related