Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
393 views
in Technique[技术] by (71.8m points)

querying between multiple django models

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)
question from:https://stackoverflow.com/questions/65891230/querying-between-multiple-django-models

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...