# Using Date function with queryset

**URL:** <https://forum.djangoproject.com/t/using-date-function-with-queryset/11201>\
**Category:** Using Django\
**Created:** [December 24, 2021, 12:02am UTC](https://forum.djangoproject.com/t/using-date-function-with-queryset/11201 "2021-12-24T00:02:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![colina83](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/colina83/32/6569_2.png) [@colina83](https://forum.djangoproject.com/u/colina83)\
**Post date:** [December 24, 2021, 12:02am UTC](https://forum.djangoproject.com/t/using-date-function-with-queryset/11201/1 "2021-12-24T00:02:47Z")

</div>

Hi All (probably Ken),

I’m creating a dashboard and one of the indicators I want to get is the change in the number of leads on a month-by-month basis, and I have a created\_date field in my model, so what I want to do is to simply get a list of all the new leads created every month

I was reading the documentation and you can use the queryset API date to get the datefields,but I’m trying to use it and is currently only returning one value:

```auto
Leads.objects.all().dates('created_at','month')

```

I’m not sure why the query is only returning 1 value?

If I have a list with all the leads and the month, then I can account for the new leads every month.

````auto
class Leads(models.Model):

    project_id = models.BigAutoField(primary_key=True, serialize=False)
    created_at = models.DateTimeField(auto_now_add=True)
    expected_revenue = MoneyField(decimal_places=2,max_digits=14, default_currency='USD')
    expected_licenses = models.IntegerField()
    country = CountryField(blank_label='(select_country)')
    status = models.CharField(choices=[('Open', 'Open'), ('Closed', 'Closed'), ('Canceled', 'Canceled'),
                                       ('Idle', 'Idle')
                                       ], max_length=10)
    estimated_closing_date = models.DateField()
    services = models.CharField(choices=[('Illumination Studies', 'Illumination Studies'),
                                  ('Training', 'Training'),('Survey Design Consultancy', 'Survey Design Consultancy'),
                                  ('Software License', 'Software License'),
                                  ('Software Development','Software Development')], max_length=40)
    agent = models.ForeignKey(Profile, default='agent',on_delete=models.CASCADE)
    company = models.ForeignKey(Company,on_delete=models.CASCADE)
    point_of_contact = models.ForeignKey(Client, default='agent',on_delete=models.CASCADE)
    updated_at = models.DateTimeField(auto_now=True)
    application = models.CharField(choices=[('O&G','O&G'),('Renewables','Renewables'),('Mining','Mining'),
                                         ('Other','Other'),('CSS','CSS')],
                                default='O&G',max_length=20)
    sub_category = models.CharField(choices=[('Wind','Wind'),('Geo-Thermal','Geo-Thermal'),('Solar','Solar'),
                                             ('Tidal','Tidal')], max_length=20, blank=True)

    @property
    def age_in_days(self):
        today = date.today()
        result = self.estimated_closing_date - today
        return result.days

    def __str__ (self):
        return f'{self.project_id}'

```
````

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [December 24, 2021, 1:07am UTC](https://forum.djangoproject.com/t/using-date-function-with-queryset/11201/2 "2021-12-24T01:07:33Z")

</div>

> [@colina83](#):
>
> I was reading the documentation and you can use the queryset API date to get the datefields,but I’m trying to use it and is currently only returning one value:
> 
> ```auto
> Leads.objects.all().dates('created_at','month')
> 
> ```

I’m not sure this is doing what you think it’s going to do.

To be clear - it’s not getting the month for every `created_at` field in the table, it’s getting a list of all the unique months. If all rows have a created\_at in the same month, you’re only going to get one value.

I get the impression that what you’re really looking to do is annotate each row with the month from the date - a different operation.

---

<div class="post-metadata">

**Author:** ![adamchainz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/adamchainz/32/26_2.png) [@adamchainz](https://forum.djangoproject.com/u/adamchainz)\
**Post date:** [December 24, 2021, 9:22am UTC](https://forum.djangoproject.com/t/using-date-function-with-queryset/11201/3 "2021-12-24T09:22:25Z")

</div>

> [@colina83](#):
>
> Hi All (probably Ken),

😂

> [@colina83](#):
>
> `Leads.objects.all().dates('created_at','month')`

FYI `.all()` is redundant. QuerySets always operate on all rows until you filter.

> [@KenWhitesell](#):
>
> I get the impression that what you’re really looking to do is annotate each row with the month from the date - a different operation.

Indeed. I think you want [`TruncMonth`](https://docs.djangoproject.com/en/4.0/ref/models/database-functions/#django.db.models.functions.TruncMonth):

`Lead.objects.annotate(created_at_month=TruncMonth('created_at'))`

> [@colina83](#):
>
> ```auto
> status = models.CharField(choices=[('Open', 'Open'), ('Closed', 'Closed'), ('Canceled', 'Canceled'),
> ('Idle', 'Idle')
> ], max_length=10)
> 
> ```

FYI from Django 3.0 you can use enumeration types for a better experience - [see my post](https://adamj.eu/tech/2020/01/27/moving-to-django-3-field-choices-enumeration-types/).

---

<div class="post-metadata">

**Author:** ![colina83](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/colina83/32/6569_2.png) [@colina83](https://forum.djangoproject.com/u/colina83)\
**Post date:** [December 24, 2021, 4:53pm UTC](https://forum.djangoproject.com/t/using-date-function-with-queryset/11201/4 "2021-12-24T16:53:08Z")

</div>

> [@adamchainz](#):
>
> Lead.objects.annotate(created\_at\_month=TruncMonth(‘created\_at’))

Thanks Adam,

That is exactly what I want it to get:

```auto
test = Leads.objects.annotate(created_at_month=TruncMonth('created_at'))
    for i in test:
        print(i.created_at_month.month)

```

---

<div class="post-metadata">

**Author:** ![colina83](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/colina83/32/6569_2.png) [@colina83](https://forum.djangoproject.com/u/colina83)\
**Post date:** [December 24, 2021, 5:16pm UTC](https://forum.djangoproject.com/t/using-date-function-with-queryset/11201/5 "2021-12-24T17:16:32Z")

</div>

Thanks a lot to all, this is what I really want it to get, a simply count of how many items are created per month

```auto
Leads.objects.annotate(created_at_month=TruncMonth('created_at')).values('created_at_month').annotate(count=Count('expected_revenue'))

```

I’m pretty impressed with the power of the querysets

Thanks a lot !
