I have a Project model similar to:
class Project(models.Model):
...
lead_analyst=models.ForeignKey(User, related_name='lead_analyst')
...
I want to use django aggregation to return all users with a count of projects per user. Something like:
models.User.objects.annotate(project_count=Count('project_set'))
Only that doesn't work because the auth.user
has no knowledge about my Project class. I get the error:
Cannot resolve keyword 'project_set' into field. Choices are: date_joined, email, employee, first_name, groups, id, is_active, is_staff, is_superuser, last_login, last_name, lead_analyst, logentry, message, password, siteprofile, submitter, user_permissions, username
Two part question, really:
How to obtain this count of Projects per
auth.user
Is there a better way to write the association between my Project class and the
auth.user
class that would make this aggregation viable?
Or should I just break into raw sql for aggregations (either via raw sql in django or a view in the database)?