Django

Code

Ticket #2210: ticket-2210.diff

File ticket-2210.diff, 4.3 kB (added by mir@noris.de, 2 years ago)

revised and updated patch

  • a/django/db/models/query.py

    old new  
    446446        where.extend(where2) 
    447447        params.extend(params2) 
    448448 
     449        # Resolve order_by dependencies before joins to order by related tables 
     450        order_by = [] 
     451        if self._order_by is not None: # use order_by = () to disable ordering 
     452            ordering_to_use = self._order_by 
     453        else: 
     454            ordering_to_use = opts.ordering 
     455 
     456        for f in handle_legacy_orderlist(ordering_to_use): 
     457            if "." in f: # dot-style field, fall back to old ordering code below 
     458                order_by = [] 
     459                break 
     460            elif f == '?': # Special case. 
     461                order_by.append(backend.get_random_function_sql()) 
     462                break 
     463            elif f.startswith('-'): 
     464                path = f[1:].split(LOOKUP_SEPARATOR) 
     465                order = "DESC" 
     466            else: 
     467                path = f.split(LOOKUP_SEPARATOR) 
     468                order = "ASC" 
     469 
     470            joins3, where3, params3 = lookup_inner(path, 'exact', False, opts, opts.db_table, None) 
     471            joins.update(joins3) 
     472 
     473            # hack to get fieldname to order by. modify lookup_inner to supply this instead. 
     474            field = where3[0].replace(' = %s', '') 
     475            order_by.append('%s %s' % (field, order)) 
     476 
    449477        # Add additional tables and WHERE clauses based on select_related. 
    450478        if self._select_related: 
    451479            fill_table_cache(opts, select, tables, where, opts.db_table, [opts.db_table]) 
     
    471499            sql.append(where and "WHERE " + " AND ".join(where)) 
    472500 
    473501        # ORDER BY clause 
    474         order_by = [] 
    475502        if self._order_by is not None: 
    476503            ordering_to_use = self._order_by 
    477504        else: 
    478505            ordering_to_use = opts.ordering 
     506        if order_by: # dont try to set up ordering again if already done 
     507            ordering_to_use = [] 
    479508        for f in handle_legacy_orderlist(ordering_to_use): 
    480             if f == '?': # Special case. 
    481                 order_by.append(backend.get_random_function_sql()) 
     509            if f.startswith('-'): 
     510                col_name = f[1:] 
     511                order = "DESC" 
    482512            else: 
    483                 if f.startswith('-'): 
    484                     col_name = f[1:] 
    485                     order = "DESC" 
    486                 else: 
    487                     col_name = f 
    488                     order = "ASC" 
    489                 if "." in col_name: 
    490                     table_prefix, col_name = col_name.split('.', 1) 
    491                     table_prefix = backend.quote_name(table_prefix) + '.' 
     513                col_name = f 
     514                order = "ASC" 
     515            if "." in col_name: 
     516                table_prefix, col_name = col_name.split('.', 1) 
     517                table_prefix = backend.quote_name(table_prefix) + '.' 
     518            else: 
     519                # Use the database table as a column prefix if it wasn't given, 
     520                # and if the requested column isn't a custom SELECT. 
     521                if "." not in col_name and col_name not in (self._select or ()): 
     522                    table_prefix = backend.quote_name(opts.db_table) + '.' 
    492523                else: 
    493                     # Use the database table as a column prefix if it wasn't given, 
    494                     # and if the requested column isn't a custom SELECT. 
    495                     if "." not in col_name and col_name not in (self._select or ()): 
    496                         table_prefix = backend.quote_name(opts.db_table) + '.' 
    497                     else: 
    498                         table_prefix = '' 
    499                 order_by.append('%s%s %s' % (table_prefix, backend.quote_name(orderfield2column(col_name, opts)), order)) 
     524                    table_prefix = '' 
     525            order_by.append('%s%s %s' % (table_prefix, backend.quote_name(orderfield2column(col_name, opts)), order)) 
    500526        if order_by: 
    501527            sql.append("ORDER BY " + ", ".join(order_by)) 
    502528