I have 3 models categories, badges, and reward. The users are awarded badges and are stored under reward. The user model is the default model provided by django.
class BadgeCategory(models.Model):
name = models.CharField(max_length=30)
class Badge(models.Model):
points = models.IntegerField(default=1)
title = models.CharField(max_length=30)
category = models.ForeignKey(BadgeCategory,on_delete=models.SET_NULL,null=True)
class Reward(models.Model):
awarded_by = models.CharField(max_length=30)
user = models.ForeignKey(User, on_delete=models.CASCADE)
badges = models.ForeignKey(Badge, on_delete=models.CASCADE)
BadgeCategory refers to the category under which the badge falls, Badge refers to various badges. and reward contains all the various badges given to users. I am looking to find a query such that I get the following format of the output
Username | category 1 | category 2 |
---|---|---|
user 1 | count(category 1) | count(category 2) |
user 2 | count(category 1) | count(category 2) |