mirror of
https://github.com/django/django.git
synced 2025-08-22 19:44:44 +00:00
Refs #33304 -- Enclosed aggregate ordering logic in an expression.
This greatly simplifies the implementation of contrib.postgres' OrderableAggMixin and allows for reuse in Window expressions.
This commit is contained in:
parent
a17becf4c7
commit
e06dc4571e
2 changed files with 36 additions and 37 deletions
|
@ -915,8 +915,8 @@ class Ref(Expression):
|
|||
class ExpressionList(Func):
|
||||
"""
|
||||
An expression containing multiple expressions. Can be used to provide a
|
||||
list of expressions as an argument to another expression, like an
|
||||
ordering clause.
|
||||
list of expressions as an argument to another expression, like a partition
|
||||
clause.
|
||||
"""
|
||||
template = '%(expressions)s'
|
||||
|
||||
|
@ -933,6 +933,26 @@ class ExpressionList(Func):
|
|||
return self.as_sql(compiler, connection, **extra_context)
|
||||
|
||||
|
||||
class OrderByList(Func):
|
||||
template = 'ORDER BY %(expressions)s'
|
||||
|
||||
def __init__(self, *expressions, **extra):
|
||||
expressions = (
|
||||
(
|
||||
OrderBy(F(expr[1:]), descending=True)
|
||||
if isinstance(expr, str) and expr[0] == '-'
|
||||
else expr
|
||||
)
|
||||
for expr in expressions
|
||||
)
|
||||
super().__init__(*expressions, **extra)
|
||||
|
||||
def as_sql(self, *args, **kwargs):
|
||||
if not self.source_expressions:
|
||||
return '', ()
|
||||
return super().as_sql(*args, **kwargs)
|
||||
|
||||
|
||||
class ExpressionWrapper(SQLiteNumericMixin, Expression):
|
||||
"""
|
||||
An expression that can wrap another expression so that it can provide
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue