Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 435543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:23:12+00:00 2026-05-12T20:23:12+00:00

I have a Project model similar to: class Project(models.Model): … lead_analyst=models.ForeignKey(User, related_name=’lead_analyst’) … I

  • 0

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:

  1. How to obtain this count of Projects per auth.user

  2. 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)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T20:23:12+00:00Added an answer on May 12, 2026 at 8:23 pm

    Nothing wrong with your models – that’s exactly how to set up that relation. The problem is simply that you’ve specified a related_name in the foreignkey, so as far as User is concerned, there’s no project_set – there’s a lead_analyst instead. In fact you can see this in the list of fields given by your error message.

    So your aggregate query should read:

    models.User.objects.annotate(project_count=Count('lead_analyst'))
    

    (I must say, I think you’ve chosen a misleading related name here – you would be better off sticking to the default, in which case your original syntax would have worked.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two Django model classes that are structured similar to the following: class
I have MySQL DB: In Django models: class Record(models.Model): schema_id = models.IntegerField() project_id =
I have a project which exposes object model to use by different types of
I have worked a bit with Django and I quite like its project/applications model
Let's say I have a model class called Project, but instead of this: class
I have an ASPNET MVC 2 project. When I use <%= Html.TextBoxFor(model => model.Login)
I have django ModelForm for model with ManyToManyField. I want to change widget for
I have a data model which is similar to this: (in another project) ________________________________
I have a Django project whereby every model inherits from a common Object model
I have a Project model and it has some text attributes, one is summary.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.