Changeset 2254
- Timestamp:
- 02/03/06 17:26:16 (3 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/magic-removal/tests/modeltests/many_to_many/models.py
r2244 r2254 36 36 >>> a1.save() 37 37 38 # Associate the Article with one Publication. set_publications() returns a 39 # boolean, representing whether any records were added or deleted. 40 >>> a1.set_publications([p1.id]) 41 True 42 43 # If we set it again, it'll return False, because the list of Publications 44 # hasn't changed. 45 >>> a1.set_publications([p1.id]) 46 False 38 # Associate the Article with a Publication. 39 >>> a1.publications.add(p1) 47 40 48 41 # Create another Article, and set it to appear in both Publications. 49 42 >>> a2 = Article(id=None, headline='NASA uses Python') 50 43 >>> a2.save() 51 >>> a2.set_publications([p1.id, p2.id]) 52 True 53 >>> a2.set_publications([p1.id]) 54 True 55 >>> a2.set_publications([p1.id, p2.id, p3.id]) 56 True 44 >>> a2.publications.add(p1, p2) 45 >>> a2.publications.add(p3) 46 47 # Add a Publication directly via publications.add by using keyword arguments. 48 >>> a2.publications.add(title='Highlights for Children') 57 49 58 50 # Article objects have access to their related Publication objects. … … 60 52 [The Python Journal] 61 53 >>> a2.publications.all() 62 [The Python Journal, Science News, Science Weekly ]54 [The Python Journal, Science News, Science Weekly, Highlights for Children] 63 55 64 56 # Publication objects have access to their related Article objects. … … 67 59 >>> p1.article_set.order_by('headline') 68 60 [Django lets you build Web apps easily, NASA uses Python] 61 >>> Publication.objects.get(id=4).article_set.all() 62 [NASA uses Python] 69 63 70 64 # We can perform kwarg queries across m2m relationships … … 80 74 [NASA uses Python] 81 75 82 # Reverse m2m queries (i.e., start at the table that doesn't have a ManyToManyField) 76 # Reverse m2m queries are supported (i.e., starting at the table that doesn't 77 # have a ManyToManyField). 83 78 >>> Publication.objects.filter(id__exact=1) 84 79 [The Python Journal] … … 87 82 88 83 >>> Publication.objects.filter(article__headline__startswith="NASA") 89 [The Python Journal, Science News, Science Weekly ]84 [The Python Journal, Science News, Science Weekly, Highlights for Children] 90 85 91 86 >>> Publication.objects.filter(article__id__exact=1) … … 98 93 >>> p1.delete() 99 94 >>> Publication.objects.all() 100 [Science News, Science Weekly ]95 [Science News, Science Weekly, Highlights for Children] 101 96 >>> a1 = Article.objects.get(pk=1) 102 97 >>> a1.publications.all()
