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?
story_setdoesn’t exist one way or another. That would’ve been the defaultrelated_nameif you hadn’t provided one. Since you did, you have to use those.OR
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 beCount('story').