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]

有帮助吗?

解决方案

You can do it like this:

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

其他提示

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

Try:

month_list = Model.objects.dates('DateField', 'month')
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top