Changeset 2610
- Timestamp:
- 04/04/06 14:40:00 (3 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/magic-removal/tests/modeltests/many_to_one/models.py
r2510 r2610 23 23 return self.headline 24 24 25 class Meta: 26 ordering = ('headline',) 25 27 26 28 API_TESTS = """ … … 61 63 1 62 64 >>> r.article_set.all() 63 [ This is a test, John's second story, Paul's story]65 [John's second story, Paul's story, This is a test] 64 66 65 67 # Add the same article to a different article set - check that it moves. … … 68 70 2 69 71 >>> r.article_set.all() 70 [ This is a test, John's second story]72 [John's second story, This is a test] 71 73 >>> r2.article_set.all() 72 74 [Paul's story] … … 80 82 1 81 83 >>> r.article_set.all() 82 [ This is a test, John's second story, Paul's story]84 [John's second story, Paul's story, This is a test] 83 85 >>> r2.article_set.all() 84 86 [] … … 95 97 >>> r.article_set = [new_article] 96 98 >>> r.article_set.all() 97 [ This is a test, John's second story]99 [John's second story, This is a test] 98 100 >>> r2.article_set.all() 99 101 [Paul's story] … … 106 108 107 109 # Reporter objects have access to their related Article objects. 108 >>> r.article_set. order_by('pub_date')109 [ This is a test, John's second story]110 >>> r.article_set.all() 111 [John's second story, This is a test] 110 112 111 113 >>> r.article_set.filter(headline__startswith='This') … … 132 134 # This works as many levels deep as you want. There's no limit. 133 135 # Find all Articles for any Reporter whose first name is "John". 134 >>> Article.objects.filter(reporter__first_name__exact='John') .order_by('pub_date')135 [ This is a test, John's second story]136 >>> Article.objects.filter(reporter__first_name__exact='John') 137 [John's second story, This is a test] 136 138 137 139 # Query twice over the related field. 138 140 >>> Article.objects.filter(reporter__first_name__exact='John', reporter__last_name__exact='Smith') 139 [ This is a test, John's second story]141 [John's second story, This is a test] 140 142 141 143 # The underlying query only makes one join when a related table is referenced twice. … … 147 149 # The automatically joined table has a predictable name. 148 150 >>> Article.objects.filter(reporter__first_name__exact='John').extra(where=["many_to_one_article__reporter.last_name='Smith'"]) 149 [ This is a test, John's second story]151 [John's second story, This is a test] 150 152 151 153 # Find all Articles for the Reporter whose ID is 1. 152 >>> Article.objects.filter(reporter__id__exact=1) .order_by('pub_date')153 [ This is a test, John's second story]154 >>> Article.objects.filter(reporter__pk=1) .order_by('pub_date')155 [ This is a test, John's second story]154 >>> Article.objects.filter(reporter__id__exact=1) 155 [John's second story, This is a test] 156 >>> Article.objects.filter(reporter__pk=1) 157 [John's second story, This is a test] 156 158 157 159 # You need two underscores between "reporter" and "id" -- not one. … … 168 170 169 171 # "pk" shortcut syntax works in a related context, too. 170 >>> Article.objects.filter(reporter__pk=1) .order_by('pub_date')171 [ This is a test, John's second story]172 >>> Article.objects.filter(reporter__pk=1) 173 [John's second story, This is a test] 172 174 173 175 # You can also instantiate an Article by passing … … 211 213 212 214 # If you delete a reporter, his articles will be deleted. 213 >>> Article.objects. order_by('headline')215 >>> Article.objects.all() 214 216 [John's second story, Paul's story, This is a test, This is a test, This is a test] 215 217 >>> Reporter.objects.order_by('first_name') 216 218 [John Smith, Paul Jones] 217 219 >>> r2.delete() 218 >>> Article.objects. order_by('headline')220 >>> Article.objects.all() 219 221 [John's second story, This is a test, This is a test, This is a test] 220 222 >>> Reporter.objects.order_by('first_name')
