henrich
henrich

Reputation: 627

How to get the reverse of a many to many relationship in Django?

I have a model for articles with various attributes like author, title, etc. One of the attributes is "comments_on" for all of the articles that the article in question (let's call it "main article" for now) is responding to. I use a reciprocal many-to-many relationship to represent this.

I know how to display on a website all of the articles that the main article comments on. I would like to know if it's possible to display all of the articles that have commented on the main article.

I assume I don't need to create a separate field in my model for this, since the relationship between the main article and other articles that comment on the main article is reflected in the same reciprocal many-to-many relationship.

Any idea what the code for this is will be? Thank you.

Upvotes: 0

Views: 48

Answers (1)

Anomitra
Anomitra

Reputation: 1159

I think you're looking for related_name.

Upvotes: 2

Related Questions