Group_by Django list

17 views
Skip to first unread message

Daniel Gómez Rico

unread,
Oct 3, 2013, 10:30:58 AM10/3/13
to django...@googlegroups.com

I have a django model:

class Person(models.Model):
    """" Modelo criado com referencia ao modelo remoto """

    name = models.CharField(max_length=300)
    age = models.PositiveIntegerField()
    born = models.DateField()

I want to group all the people by birthday (group by born date). I'm trying to get a dictionary of dictionaries, like this:

{ 
    02-17:[
            {
               name:'Daniel',
               age:22
            },
            {
               name:'Pedro',
               age:23
            },
          ],
    05-24:[
            {
               name:'Juan',
               age:44
            }
          ]
}

One option is:

query = Person.objects.all().query
query.group_by = ['born']

for item in QuerySet(query=query, model=Person):
    print item

but it returns a list with all objects ordered by date... no grouped.

Can I do this by django ORM or with sql raw? I will have to do it by myself?

Thanks

http://stackoverflow.com/questions/19161671/group-by-django-list

Reply all
Reply to author
Forward
0 new messages