I have a Model Poller that has a field category. I have a filtered queryset of Poller. Now I would like to count the objects for each category like so:
Poller 1: poller_category - Sport
Poller 2: poller_category - Sport
Poller 3: poller_category - Tech
Poller 4: poller_category - Tech
Poller 5: poller_category - Fashion
should return a dictionary somewhat to counts = {'Sport' : '2', 'Tech' : '2', 'Fashion' : '1'}
# Model
class Poller(models.Model):
poller_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
poller_category = models.ForeignKey(Category, on_delete=models.CASCADE, null=True)
created_by = models.ForeignKey(Account, on_delete=models.CASCADE)
class Category(models.Model):
"""
Model to define categories and their color styles
"""
poller_category = models.CharField(max_length=30)
category_color = models.CharField(max_length=15, blank=True)
# View / query
qs = Poller.objects.filter(
created_by__username=username).order_by(
'-poller_category')
Not sure but the ordering itself might be obsolet probably.