Reputation: 37
models.py
from django.db import models
from collections import Counter
from django.db.models import Count
from itertools import groupby
Gender = (
('Male', 'MALE'),
('Female', 'FEMALE'),
)
Diseases = (
('Cholera', 'CHOLERA'),
('HIV', 'HIV'),
('Malaria', 'MALARIA'),
('Typhoid', 'TYPHOID'),
('Measles', 'MEASLES'),
)
class MyModel(models.Model):
Gender = models.CharField(max_length=16, choices=Gender, default='MALE')
Diseases = models.CharField(max_length=16, choices=Diseases, default='MALARIA')
vote = models.IntegerField(default=0)
def __str__(self):
r=0
return u'%s %s' % (self.Gender, self.Diseases)
Now this returns this in the admin django panel
Male Measles
Male Measles
Male Cholera
Female Cholera
Female Typhoid
Female Typhoid
But I want to group it so it looks something like this:
Male malaria 2
Male Measles 2
Female Typhoid 3
in this admin panel instead of listing all of it like it does now
Upvotes: 1
Views: 312
Reputation: 3611
You can utilize the values
and annotate
functions for grouping values
MyModel.objects.values('Gender, Diseases').annotate(Count('Gender'), Count('Diseases'))
However, grouping something in the Admin Panel doesn't really makes sense if you want to be able to edit the data. Grouping something removes the identity/primary key of each element and would make it unable to actually choose one element in the objects view.
Upvotes: 1