Module Expr Str ImportFrom alias ClassDef bases AttributeLoad NameLoad attr body Assign NameStore Call AttributeLoad NameLoad attr keyword Num ClassDef bases body Assign NameStore TupleLoad Str decorator_list FunctionDef arguments args NameParam defaults body Return AttributeLoad NameLoad attr decorator_list decorator_list ClassDef bases AttributeLoad NameLoad attr body Assign NameStore Call AttributeLoad NameLoad attr keyword Num Assign NameStore Call AttributeLoad NameLoad attr Assign NameStore Call AttributeLoad NameLoad attr NameLoad keyword Str Assign NameStore Call AttributeLoad NameLoad attr NameLoad keyword Str ClassDef bases body Assign NameStore TupleLoad Str decorator_list FunctionDef arguments args NameParam defaults body Return AttributeLoad NameLoad attr decorator_list decorator_list Assign NameStore Dict Str Str
N/A N/A 
20. Multiple many-to-many relationships between the same two tables

In this example, an Article can have many Categories (as "primary") and many
Categories (as "secondary").

Set ``related_name`` to designate what the reverse relationship is called.
 django.db models Category N/A N/A models Model N/A N/A name N/A N/A models CharField maxlength 20 Meta N/A N/A N/A ordering N/A name N/A __str__ N/A N/A self N/A N/A N/A N/A self name N/A N/A Article N/A N/A models Model N/A N/A headline N/A N/A models CharField maxlength 50 N/A pub_date N/A N/A models DateTimeField N/A primary_categories N/A N/A models ManyToManyField Category related_name primary_article_set N/A secondary_categories N/A N/A models ManyToManyField Category related_name secondary_article_set Meta N/A N/A N/A ordering N/A pub_date N/A __str__ N/A N/A self N/A N/A N/A N/A self headline N/A N/A N/A __test__ N/A API_TESTS 
>>> from datetime import datetime

>>> c1 = Category(name='Sports')
>>> c1.save()
>>> c2 = Category(name='News')
>>> c2.save()
>>> c3 = Category(name='Crime')
>>> c3.save()
>>> c4 = Category(name='Life')
>>> c4.save()

>>> a1 = Article(headline='Area man steals', pub_date=datetime(2005, 11, 27))
>>> a1.save()
>>> a1.primary_categories.add(c2, c3)
>>> a1.secondary_categories.add(c4)

>>> a2 = Article(headline='Area man runs', pub_date=datetime(2005, 11, 28))
>>> a2.save()
>>> a2.primary_categories.add(c1, c2)
>>> a2.secondary_categories.add(c4)

>>> a1.primary_categories.all()
[<Category: Crime>, <Category: News>]

>>> a2.primary_categories.all()
[<Category: News>, <Category: Sports>]

>>> a1.secondary_categories.all()
[<Category: Life>]


>>> c1.primary_article_set.all()
[<Article: Area man runs>]
>>> c1.secondary_article_set.all()
[]
>>> c2.primary_article_set.all()
[<Article: Area man steals>, <Article: Area man runs>]
>>> c2.secondary_article_set.all()
[]
>>> c3.primary_article_set.all()
[<Article: Area man steals>]
>>> c3.secondary_article_set.all()
[]
>>> c4.primary_article_set.all()
[]
>>> c4.secondary_article_set.all()
[<Article: Area man steals>, <Article: Area man runs>]
