Ticket #6498: 0045-Add-case-insensitive-model-ordering.patch

File 0045-Add-case-insensitive-model-ordering.patch, 4.9 KB (added by Bastian Kleineidam <calvin@…>, 8 years ago)
  • django/core/management/validation.py

    From 03da70d594780f6e98f4b5c62d9d5dd5e071cf5a Mon Sep 17 00:00:00 2001
    From: Bastian Kleineidam <calvin@debian.org>
    Date: Mon, 28 Jan 2008 10:05:53 +0100
    Subject: Add case insensitive model ordering
    
    Add support for case insensitive ordering of model objects by using
    .order_by("*name") or .order_by("*-name") syntax.
    
    Signed-off-by: Bastian Kleineidam <calvin@debian.org>
    
    diff --git a/django/core/management/validation.py b/django/core/management/validation.py
    index bc9faae..f052510 100644
    a b def get_validation_errors(outfile, app=None): 
    196196        if opts.ordering:
    197197            for field_name in opts.ordering:
    198198                if field_name == '?': continue
     199                if field_name.startswith('*'):
     200                    field_name = field_name[1:]
    199201                if field_name.startswith('-'):
    200202                    field_name = field_name[1:]
    201203                if opts.order_with_respect_to and field_name == '_order':
  • django/db/models/query.py

    diff --git a/django/db/models/query.py b/django/db/models/query.py
    index c9dcccb..c93f5d5 100644
    a b def orderlist2sql(order_list, opts, prefix=''): 
    6969        prefix = qn(prefix[:-1]) + '.'
    7070    output = []
    7171    for f in handle_legacy_orderlist(order_list):
    72         if f.startswith('-'):
    73             output.append('%s%s DESC' % (prefix, qn(orderfield2column(f[1:], opts))))
    74         elif f == '?':
     72        if f == '?':
    7573            output.append(connection.ops.random_function_sql())
     74            continue
     75        if f.startswith('*'):
     76            f = f[1:]
     77            func = "LOWER"
     78        else:
     79            func = None
     80        if f.startswith('-'):
     81            f = f[1:]
     82            order = " DESC"
    7683        else:
    77             output.append('%s%s ASC' % (prefix, qn(orderfield2column(f, opts))))
     84            order = " ASC"
     85        name = "%s%s" % (prefix, qn(orderfield2column(f, opts)))
     86        if func:
     87            name = "%s(%s)" % (func, name)
     88        output.append('%s %s' % (name, order))
    7889    return ', '.join(output)
    7990
    8091def quote_only_if_word(word):
    class _QuerySet(object): 
    536547        for f in handle_legacy_orderlist(ordering_to_use):
    537548            if f == '?': # Special case.
    538549                order_by.append(connection.ops.random_function_sql())
     550                continue
     551            if f.startswith('*'):
     552                f = f[1:]
     553                func = "LOWER"
    539554            else:
    540                 if f.startswith('-'):
    541                     col_name = f[1:]
    542                     order = "DESC"
    543                 else:
    544                     col_name = f
    545                     order = "ASC"
    546                 if "." in col_name:
    547                     table_prefix, col_name = col_name.split('.', 1)
    548                     table_prefix = qn(table_prefix) + '.'
     555                func = None
     556            if f.startswith('-'):
     557                col_name = f[1:]
     558                order = "DESC"
     559            else:
     560                col_name = f
     561                order = "ASC"
     562            if "." in col_name:
     563                table_prefix, col_name = col_name.split('.', 1)
     564                table_prefix = qn(table_prefix) + '.'
     565            else:
     566                # Use the database table as a column prefix if it wasn't given,
     567                # and if the requested column isn't a custom SELECT.
     568                if "." not in col_name and col_name not in (self._select or ()):
     569                    table_prefix = qn(opts.db_table) + '.'
    549570                else:
    550                     # Use the database table as a column prefix if it wasn't given,
    551                     # and if the requested column isn't a custom SELECT.
    552                     if "." not in col_name and col_name not in (self._select or ()):
    553                         table_prefix = qn(opts.db_table) + '.'
    554                     else:
    555                         table_prefix = ''
    556                 order_by.append('%s%s %s' % (table_prefix, qn(orderfield2column(col_name, opts)), order))
     571                    table_prefix = ''
     572            name = "%s%s" % (table_prefix, qn(orderfield2column(col_name, opts)))
     573            if func:
     574                name = "%s(%s)" % (func, name)
     575            order_by.append('%s %s' % (name, order))
    557576        if order_by:
    558577            sql.append("ORDER BY " + ", ".join(order_by))
    559578
  • tests/modeltests/ordering/models.py

    diff --git a/tests/modeltests/ordering/models.py b/tests/modeltests/ordering/models.py
    index 3e651d4..c390edc 100644
    a b __test__ = {'API_TESTS':""" 
    6464# don't know what order the output will be in.
    6565>>> Article.objects.order_by('?')
    6666[...]
     67
     68# order case insensitive
     69>>> a = Article.objects.get(headline='Article 3')
     70>>> a.headline = 'article 3'
     71>>> a.save()
     72>>> Article.objects.order_by('*headline')
     73[<Article: Article 1>, <Article: Article 2>, <Article: article 3>, <Article: Article 4>]
    6774"""}
Back to Top