I have the following models:
class ProjectUser(models.Model):
categories = models.ManyToManyField('UserCategory', blank=True, null=True)
user_id = models.PositiveIntegerField(db_index=True)
name = models.CharField(max_length=80)
actual_rank = models.FloatField(default=0)
class UserCategory(models.Model):
name = models.CharField(max_length=100)
What I’d like to do is get the categories name and the number of times they appear in the users divided by the length of another object.
I’m doing it creating a categories dictionary {‘category’: ocurrences } and then iterating it and dividing the ocurrences by a number to get the category rank.
Maybe this is a good way to do it, but I’d like to know if it could be done directly using some queryset methods. I’m doing similar things in a lot of places and finding a better and more succint way to solve it would be great.
This should get all users with the category name in their categories, count that number of users, then divide that number by the length you’re getting somewhere else.
categorybeing the current one you’re trying to analyse.