Ticket #5293: sql_function_in_order_by.diff

File sql_function_in_order_by.diff, 1.6 KB (added by rb.online@…, 8 years ago)

patch

  • django/db/models/query.py

    diff -ru django/db/models/query.py /Library/Frameworks/Python.framework/Versions/Current/lib/python2.5/site-packages/django/db/models/query.py
    old new  
    529529                else:
    530530                    col_name = f
    531531                    order = "ASC"
     532                if "(" in col_name:
     533                    b, e = col_name.find("("), col_name.find(")")
     534                    function = col_name[:b].upper()
     535                    col_name = col_name[b+1:e]
     536                else:
     537                    function = None
    532538                if "." in col_name:
    533539                    table_prefix, col_name = col_name.split('.', 1)
    534540                    table_prefix = backend.quote_name(table_prefix) + '.'
     
    539545                        table_prefix = backend.quote_name(opts.db_table) + '.'
    540546                    else:
    541547                        table_prefix = ''
    542                 order_by.append('%s%s %s' % (table_prefix, backend.quote_name(orderfield2column(col_name, opts)), order))
     548                if function:
     549                    order_by.append('%s(%s%s) %s' % (function, table_prefix, backend.quote_name(orderfield2column(col_name, opts)), order))
     550                else:
     551                    order_by.append('%s%s %s' % (table_prefix, backend.quote_name(orderfield2column(col_name, opts)), order))
    543552        if order_by:
    544553            sql.append("ORDER BY " + ", ".join(order_by))
Back to Top