p1 = Publication(title='The Python Journal') p1.save() a1 = Article(headline='Django lets you build Web apps easily') a1.save() a1.publications.add(p1) # Adding a second time is OK, it will not duplicate the relation: a1.publications.add(p1)
Here is what the above code is Doing:
1. Creating a Publication object with a title of ‘The Python Journal’.
2. Creating an Article object with a headline of ‘Django lets you build Web apps easily’.
3. Adding the Article object to the Publication object’s article_set.
4. Adding the Publication object to the Article object’s publications.