Changeset 7477 for django/trunk/tests/modeltests/many_to_one
- Timestamp:
- 04/26/08 21:50:16 (9 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/tests/modeltests/many_to_one/models.py
r5876 r7477 146 146 147 147 # The underlying query only makes one join when a related table is referenced twice. 148 >>> query = Article.objects.filter(reporter__first_name__exact='John', reporter__last_name__exact='Smith')149 >>> null, sql, null = query._get_sql_clause()148 >>> queryset = Article.objects.filter(reporter__first_name__exact='John', reporter__last_name__exact='Smith') 149 >>> sql = queryset.query.as_sql()[0] 150 150 >>> sql.count('INNER JOIN') 151 151 1 152 152 153 153 # The automatically joined table has a predictable name. 154 >>> Article.objects.filter(reporter__first_name__exact='John').extra(where=["many_to_one_ article__reporter.last_name='Smith'"])154 >>> Article.objects.filter(reporter__first_name__exact='John').extra(where=["many_to_one_reporter.last_name='Smith'"]) 155 155 [<Article: John's second story>, <Article: This is a test>] 156 156 157 157 # And should work fine with the unicode that comes out of 158 158 # newforms.Form.cleaned_data 159 >>> Article.objects.filter(reporter__first_name__exact='John').extra(where=["many_to_one_ article__reporter.last_name='%s'" % u'Smith'])159 >>> Article.objects.filter(reporter__first_name__exact='John').extra(where=["many_to_one_reporter.last_name='%s'" % u'Smith']) 160 160 [<Article: John's second story>, <Article: This is a test>] 161 161 … … 180 180 Traceback (most recent call last): 181 181 ... 182 TypeError: Cannot resolve keyword 'reporter_id' into field. Choices are: id, headline, pub_date, reporter182 FieldError: Cannot resolve keyword 'reporter_id' into field. Choices are: headline, id, pub_date, reporter 183 183 184 184 # You need to specify a comparison clause … … 186 186 Traceback (most recent call last): 187 187 ... 188 TypeError: Cannot resolve keyword 'reporter_id' into field. Choices are: id, headline, pub_date, reporter188 FieldError: Cannot resolve keyword 'reporter_id' into field. Choices are: headline, id, pub_date, reporter 189 189 190 190 # You can also instantiate an Article by passing … … 250 250 >>> Reporter.objects.filter(article__reporter=r).distinct() 251 251 [<Reporter: John Smith>] 252 253 # It's possible to use values() calls across many-to-one relations. (Note, too, that we clear the ordering here so as not to drag the 'headline' field into the columns being used to determine uniqueness.) 254 >>> d = {'reporter__first_name': u'John', 'reporter__last_name': u'Smith'} 255 >>> list(Article.objects.filter(reporter=r).distinct().order_by().values('reporter__first_name', 'reporter__last_name')) == [d] 256 True 252 257 253 258 # If you delete a reporter, his articles will be deleted.
