How to count how many itens were created by day, week and month from DateTimeField()

Having the following model:

class Objects(models.Model):
    created_at = models.DateTimeField(auto_now_add=True)

How can I count how many Objects were created by day, week and month, returning an integer number for each query?

This is my solution

def lookups(self, request, model_admin):
    ff =int(JalaliDate.today().strftime('%Y'))
    print(ff)
    return (
        ('0', 'today'),
        ('1', '2 days ago'),
        ('2', '3 days ago'),
        ('3', '7 days ago'),
        ('8', 'last month'),
        ('4', 'this month'),

    )




def queryset(self, request, queryset):
    
    if self.value() == '0':
        return queryset.filter(start_date=date.today())
    if self.value() == '1':
        return queryset.filter(start_date__range=[ date.today() - timedelta(days=1),date.today()  ])
    if self.value() == '2':
        return queryset.filter(start_date__range=[ date.today() - timedelta(days=2),date.today()  ])
    if self.value() == '3':
        return queryset.filter(start_date__range=[ date.today() - timedelta(days=6),date.today()  ])
    if self.value() == '8':
        return queryset.filter(start_date__range=[ date.today() - timedelta(days=int(JalaliDate.today().strftime('%d')) + 30),date.today() - timedelta(days=int(JalaliDate.today().strftime('%d')) ) ])
    if self.value() == '4':
        return queryset.filter(start_date__range=[ date.today() - timedelta(days=int(JalaliDate.today().strftime('%d'))),date.today() ])
Back to Top