Get months from datefield in django

4.1k Views Asked by At

If I have a Datefield in django model, how can I get all possible months values from model objects? so if I have object1 with date = 22.01.2013 and object2 with date = 23.05.2013, I need to get list of months [01, 05] or [1, 5]

2

There are 2 best solutions below

0
On BEST ANSWER

You can do it like this:

months = [i.month for i in MyModel.objects.values_list('date', flat=True)]
2
On

Hey I think the dates function is what you are looking for:

Try:

month_list = Model.objects.dates('DateField', 'month')