| 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 | |
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) + '.' |
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)) |