1
0
mirror of https://github.com/django/django.git synced 2024-12-23 01:25:58 +00:00

Refs #35064 -- Made OrderableAggMixin avoid creating empty OrderByList.

This paves the way for making OrderByList a simple shim over
ExpressionList which requires at least a single item to be provided.
This commit is contained in:
Simon Charette 2023-12-29 01:28:19 -05:00 committed by Mariusz Felisiak
parent bb560651c4
commit 90d365d869

View File

@ -3,18 +3,21 @@ from django.db.models.expressions import OrderByList
class OrderableAggMixin: class OrderableAggMixin:
def __init__(self, *expressions, ordering=(), **extra): def __init__(self, *expressions, ordering=(), **extra):
if isinstance(ordering, (list, tuple)): if not ordering:
self.order_by = None
elif isinstance(ordering, (list, tuple)):
self.order_by = OrderByList(*ordering) self.order_by = OrderByList(*ordering)
else: else:
self.order_by = OrderByList(ordering) self.order_by = OrderByList(ordering)
super().__init__(*expressions, **extra) super().__init__(*expressions, **extra)
def resolve_expression(self, *args, **kwargs): def resolve_expression(self, *args, **kwargs):
self.order_by = self.order_by.resolve_expression(*args, **kwargs) if self.order_by is not None:
self.order_by = self.order_by.resolve_expression(*args, **kwargs)
return super().resolve_expression(*args, **kwargs) return super().resolve_expression(*args, **kwargs)
def get_source_expressions(self): def get_source_expressions(self):
if self.order_by.source_expressions: if self.order_by is not None:
return super().get_source_expressions() + [self.order_by] return super().get_source_expressions() + [self.order_by]
return super().get_source_expressions() return super().get_source_expressions()
@ -24,6 +27,9 @@ class OrderableAggMixin:
return super().set_source_expressions(exprs) return super().set_source_expressions(exprs)
def as_sql(self, compiler, connection): def as_sql(self, compiler, connection):
order_by_sql, order_by_params = compiler.compile(self.order_by) if self.order_by is not None:
order_by_sql, order_by_params = compiler.compile(self.order_by)
else:
order_by_sql, order_by_params = "", ()
sql, sql_params = super().as_sql(compiler, connection, ordering=order_by_sql) sql, sql_params = super().as_sql(compiler, connection, ordering=order_by_sql)
return sql, (*sql_params, *order_by_params) return sql, (*sql_params, *order_by_params)