Ticket #1730: 1730.patch

File 1730.patch, 1.7 KB (added by Cheng Zhang <czhang.cmu+web@…>, 19 years ago)
  • django/db/models/query.py

    ==== 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
    ==================================================================
     
    427427                else:
    428428                    # Use the database table as a column prefix if it wasn't given,
    429429                    # 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():
    431431                        table_prefix = backend.quote_name(opts.db_table) + '.'
    432432                    else:
    433433                        table_prefix = ''
  • tests/modeltests/many_to_one/models.py

    === tests/modeltests/many_to_one/models.py
    ==================================================================
     
    211211>>> Reporter.objects.filter(article__reporter__first_name__startswith='John').distinct()
    212212[John Smith]
    213213
     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
    214222# If you delete a reporter, his articles will be deleted.
    215223>>> Article.objects.all()
    216224[John's second story, Paul's story, This is a test, This is a test, This is a test]
Back to Top