Ticket #245: no-legacy-ordering.diff

File no-legacy-ordering.diff, 1.9 KB (added by Paul Bx <pb@…>, 17 years ago)

a patch, though it may be outdated by the time 1.0 arrives :)

  • django/db/models/query.py

     
    3535# HELPER FUNCTIONS #
    3636####################
    3737
    38 # Django currently supports two forms of ordering.
    39 # Form 1 (deprecated) example:
    40 #     order_by=(('pub_date', 'DESC'), ('headline', 'ASC'), (None, 'RANDOM'))
    41 # Form 2 (new-style) example:
    42 #     order_by=('-pub_date', 'headline', '?')
    43 # Form 1 is deprecated and will no longer be supported for Django's first
    44 # official release. The following code converts from Form 1 to Form 2.
    45 
    46 LEGACY_ORDERING_MAPPING = {'ASC': '_', 'DESC': '-_', 'RANDOM': '?'}
    47 
    48 def handle_legacy_orderlist(order_list):
    49     if not order_list or isinstance(order_list[0], basestring):
    50         return order_list
    51     else:
    52         import warnings
    53         new_order_list = [LEGACY_ORDERING_MAPPING[j.upper()].replace('_', str(i)) for i, j in order_list]
    54         warnings.warn("%r ordering syntax is deprecated. Use %r instead." % (order_list, new_order_list), DeprecationWarning)
    55         return new_order_list
    56 
    5738def orderfield2column(f, opts):
    5839    try:
    5940        return opts.get_field(f, False).column
     
    6445    if prefix.endswith('.'):
    6546        prefix = backend.quote_name(prefix[:-1]) + '.'
    6647    output = []
    67     for f in handle_legacy_orderlist(order_list):
     48    for f in order_list:
    6849        if f.startswith('-'):
    6950            output.append('%s%s DESC' % (prefix, backend.quote_name(orderfield2column(f[1:], opts))))
    7051        elif f == '?':
     
    526507            ordering_to_use = self._order_by
    527508        else:
    528509            ordering_to_use = opts.ordering
    529         for f in handle_legacy_orderlist(ordering_to_use):
     510        for f in ordering_to_use:
    530511            if f == '?': # Special case.
    531512                order_by.append(backend.get_random_function_sql())
    532513            else:
Back to Top