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

  • Home
  • SEARCH
  • 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 6915411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:28:19+00:00 2026-05-27T09:28:19+00:00

Following is my model: class Story(models.Model): author = models.ForeignKey(User, related_name=’author_id’, default=1) source = models.ForeignKey(User,

  • 0

Following is my model:

class Story(models.Model):
    author = models.ForeignKey(User, related_name='author_id', default=1)
    source = models.ForeignKey(User, related_name='source_id', default=2)

User is the django provided model.

Now I need to find out number of articles authored and sourced by each user.

I thought of using story_set with user object:

res = User.objects.annotate(Count('story_set'))

However, there are two columns in story referencing User. Hence, Django won’t know which one to use?

Can anyone help me on this?

  • 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-27T09:28:19+00:00Added an answer on May 27, 2026 at 9:28 am

    story_set doesn’t exist one way or another. That would’ve been the default related_name if you hadn’t provided one. Since you did, you have to use those.

    res = User.objects.annotate(Count('author_id'))
    

    OR

    res = User.objects.annotate(Count('source_id'))
    

    So, that’s how Django knows the difference.

    FYI: if you had used the default (so you accessed stories via .story_set.all(), you don’t use the “_set” part in queries. It would just be Count('story').

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

Sidebar

Related Questions

hi i have to following model class Match(models.Model): Team_one = models.ForeignKey('Team', related_name='Team_one') Team_two =
I have the following models: class Author(db.Model): name = db.StringProperty() class Story(db.Model): author =
I have the following model: class User: name = models.CharField( max_length = 200 )
In a Django UserProfile model, I am trying to do the following: class UserProfile(models.Model):
I have a following model: class Car(models.Model): make = models.CharField(max_length=40) mileage_limit = models.IntegerField() mileage
Given the following model: class Project(models.Model): project_name = models.CharField(max_length=255) abstract = models.TextField(blank=True, null=True) full_description
In the following model: class header(models.Model): title = models.CharField(max_length = 255) created_by = models.CharField(max_length
Let's say I have the following model: class Contest: title = models.CharField( max_length =
In my GWT app I have the following model class: import com.google.gwt.user.client.rpc.IsSerializable; public class
This is just an example, but given the following model: class Foo(models.model): bar =

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.