Return the table alias (the name might be ambiguous, the alias will not be) and column name for ordering by the given 'name' parameter. The 'name' is of the form 'field1__field2__...__fieldN'.
(
self, name, opts, alias=None, default_order="ASC", already_seen=None
)
| 1058 | return result, params |
| 1059 | |
| 1060 | def find_ordering_name( |
| 1061 | self, name, opts, alias=None, default_order="ASC", already_seen=None |
| 1062 | ): |
| 1063 | """ |
| 1064 | Return the table alias (the name might be ambiguous, the alias will |
| 1065 | not be) and column name for ordering by the given 'name' parameter. |
| 1066 | The 'name' is of the form 'field1__field2__...__fieldN'. |
| 1067 | """ |
| 1068 | name, order = get_order_dir(name, default_order) |
| 1069 | descending = order == "DESC" |
| 1070 | pieces = name.split(LOOKUP_SEP) |
| 1071 | ( |
| 1072 | field, |
| 1073 | targets, |
| 1074 | alias, |
| 1075 | joins, |
| 1076 | path, |
| 1077 | opts, |
| 1078 | transform_function, |
| 1079 | ) = self._setup_joins(pieces, opts, alias) |
| 1080 | |
| 1081 | # If we get to this point and the field is a relation to another model, |
| 1082 | # append the default ordering for that model unless it is the pk |
| 1083 | # shortcut or the attribute name of the field that is specified or |
| 1084 | # there are transforms to process. |
| 1085 | if ( |
| 1086 | field.is_relation |
| 1087 | and opts.ordering |
| 1088 | and getattr(field, "attname", None) != pieces[-1] |
| 1089 | and name != "pk" |
| 1090 | and not getattr(transform_function, "has_transforms", False) |
| 1091 | ): |
| 1092 | # Firstly, avoid infinite loops. |
| 1093 | already_seen = already_seen or set() |
| 1094 | join_tuple = tuple( |
| 1095 | getattr(self.query.alias_map[j], "join_cols", None) for j in joins |
| 1096 | ) |
| 1097 | if join_tuple in already_seen: |
| 1098 | raise FieldError("Infinite loop caused by ordering.") |
| 1099 | already_seen.add(join_tuple) |
| 1100 | |
| 1101 | results = [] |
| 1102 | for item in opts.ordering: |
| 1103 | if hasattr(item, "resolve_expression") and not isinstance( |
| 1104 | item, OrderBy |
| 1105 | ): |
| 1106 | item = item.desc() if descending else item.asc() |
| 1107 | if isinstance(item, OrderBy): |
| 1108 | results.append( |
| 1109 | (item.prefix_references(f"{name}{LOOKUP_SEP}"), False) |
| 1110 | ) |
| 1111 | continue |
| 1112 | results.extend( |
| 1113 | (expr.prefix_references(f"{name}{LOOKUP_SEP}"), is_ref) |
| 1114 | for expr, is_ref in self.find_ordering_name( |
| 1115 | item, opts, alias, order, already_seen |
| 1116 | ) |
| 1117 | ) |
no test coverage detected