2👍
✅
As it shows in the official django docs, you can simply add the new article by adding it to the series set (of related articles)
class ArticlesSerie(models.Model):
title = models.CharField(max_length=120)
def add_article(self, article):
self.article_set.add(article)
As you can see, it’s probably not worth it to do it in a specific method since it’s a one-liner, unless you want to perform further checking (e.g. on the article’s fields)
Note: you might want to consider using create
instead of add
, if you do not need to instantiate the article beforehand.
Source:stackexchange.com