[Answer]-Django database relationships โ€“ many-to-many, many-to-one

1๐Ÿ‘

One reporter could have many articles, but an article just has one
reporter.

# This will get the reporter of an article
article.reporter
# This will get all the articles of a reporter
reporter.article_set.all()
# You cannot add another reporter to an article
article.reporter.add(r) # This will raise an Error!

On the other hand,

One article could have many publications, and a publication could be related to many articles.

# This will get all the publications of an article
article.publications.all()
# This will get all the related articles of a publication
publication.article_set.all()
# You CAN add another publication to an article
article.publications.add(p) # No Error here
๐Ÿ‘คjuliomalegria

Leave a comment