==== Patch <#1730> level 1
Source: [No source]
Target: bcc190cf-cafb-0310-a4f2-bffc1f526a37:/django/branches/magic-removal:2799 [mirrored]
(http://code.djangoproject.com/svn/django)
Log:
Fix order by on custom column
=== django/db/models/query.py
==================================================================
|
|
|
427 | 427 | else: |
428 | 428 | # Use the database table as a column prefix if it wasn't given, |
429 | 429 | # and if the requested column isn't a custom SELECT. |
430 | | if "." not in col_name and col_name not in [k[0] for k in (self._select or ())]: |
| 430 | if "." not in col_name and col_name not in self._select.keys(): |
431 | 431 | table_prefix = backend.quote_name(opts.db_table) + '.' |
432 | 432 | else: |
433 | 433 | table_prefix = '' |
=== tests/modeltests/many_to_one/models.py
==================================================================
|
|
|
211 | 211 | >>> Reporter.objects.filter(article__reporter__first_name__startswith='John').distinct() |
212 | 212 | [John Smith] |
213 | 213 | |
| 214 | # Order by custom column |
| 215 | >>> select={'articles': 'SELECT COUNT(*) FROM many_to_one_article WHERE reporter_id=many_to_one_reporter.id'} |
| 216 | >>> r=Reporter.objects.extra(select=select).order_by('articles') |
| 217 | >>> r |
| 218 | [Paul Jones, John Smith] |
| 219 | >>> [e.articles for e in r] |
| 220 | [1, 4] |
| 221 | |
214 | 222 | # If you delete a reporter, his articles will be deleted. |
215 | 223 | >>> Article.objects.all() |
216 | 224 | [John's second story, Paul's story, This is a test, This is a test, This is a test] |