mirror of
https://github.com/django/django.git
synced 2025-09-18 06:59:12 +00:00
Refs #35444 -- Removed contrib.postgres aggregates ordering kwarg per deprecation timeline.
This commit is contained in:
parent
32e266dc5b
commit
7d7e5cd055
@ -6,8 +6,6 @@ from django.db.models import StringAgg as _StringAgg
|
|||||||
from django.db.models import Value
|
from django.db.models import Value
|
||||||
from django.utils.deprecation import RemovedInDjango70Warning
|
from django.utils.deprecation import RemovedInDjango70Warning
|
||||||
|
|
||||||
from .mixins import _DeprecatedOrdering
|
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"ArrayAgg",
|
"ArrayAgg",
|
||||||
"BitAnd",
|
"BitAnd",
|
||||||
@ -20,9 +18,7 @@ __all__ = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
# RemovedInDjango61Warning: When the deprecation ends, replace with:
|
class ArrayAgg(Aggregate):
|
||||||
# class ArrayAgg(Aggregate):
|
|
||||||
class ArrayAgg(_DeprecatedOrdering, Aggregate):
|
|
||||||
function = "ARRAY_AGG"
|
function = "ARRAY_AGG"
|
||||||
allow_distinct = True
|
allow_distinct = True
|
||||||
allow_order_by = True
|
allow_order_by = True
|
||||||
@ -54,19 +50,15 @@ class BoolOr(Aggregate):
|
|||||||
output_field = BooleanField()
|
output_field = BooleanField()
|
||||||
|
|
||||||
|
|
||||||
# RemovedInDjango61Warning: When the deprecation ends, replace with:
|
class JSONBAgg(Aggregate):
|
||||||
# class JSONBAgg(Aggregate):
|
|
||||||
class JSONBAgg(_DeprecatedOrdering, Aggregate):
|
|
||||||
function = "JSONB_AGG"
|
function = "JSONB_AGG"
|
||||||
allow_distinct = True
|
allow_distinct = True
|
||||||
allow_order_by = True
|
allow_order_by = True
|
||||||
output_field = JSONField()
|
output_field = JSONField()
|
||||||
|
|
||||||
|
|
||||||
# RemovedInDjango61Warning: When the deprecation ends, replace with:
|
|
||||||
# class StringAgg(_StringAgg):
|
|
||||||
# RemovedInDjango70Warning: When the deprecation ends, remove completely.
|
# RemovedInDjango70Warning: When the deprecation ends, remove completely.
|
||||||
class StringAgg(_DeprecatedOrdering, _StringAgg):
|
class StringAgg(_StringAgg):
|
||||||
|
|
||||||
def __init__(self, expression, delimiter, **extra):
|
def __init__(self, expression, delimiter, **extra):
|
||||||
if isinstance(delimiter, str):
|
if isinstance(delimiter, str):
|
||||||
|
@ -1,29 +1,11 @@
|
|||||||
# RemovedInDjango70Warning: When the deprecation ends, remove completely.
|
# RemovedInDjango70Warning: When the deprecation ends, remove completely.
|
||||||
import warnings
|
import warnings
|
||||||
|
|
||||||
from django.utils.deprecation import RemovedInDjango61Warning, RemovedInDjango70Warning
|
from django.utils.deprecation import RemovedInDjango70Warning
|
||||||
|
|
||||||
|
|
||||||
# RemovedInDjango61Warning.
|
|
||||||
class _DeprecatedOrdering:
|
|
||||||
def __init__(self, *expressions, ordering=(), order_by=(), **extra):
|
|
||||||
if ordering:
|
|
||||||
warnings.warn(
|
|
||||||
"The ordering argument is deprecated. Use order_by instead.",
|
|
||||||
category=RemovedInDjango61Warning,
|
|
||||||
stacklevel=2,
|
|
||||||
)
|
|
||||||
if order_by:
|
|
||||||
raise TypeError("Cannot specify both order_by and ordering.")
|
|
||||||
order_by = ordering
|
|
||||||
|
|
||||||
super().__init__(*expressions, order_by=order_by, **extra)
|
|
||||||
|
|
||||||
|
|
||||||
# RemovedInDjango70Warning.
|
# RemovedInDjango70Warning.
|
||||||
# RemovedInDjango61Warning: When the deprecation ends, replace with:
|
class OrderableAggMixin:
|
||||||
# class OrderableAggMixin:
|
|
||||||
class OrderableAggMixin(_DeprecatedOrdering):
|
|
||||||
allow_order_by = True
|
allow_order_by = True
|
||||||
|
|
||||||
def __init_subclass__(cls, /, *args, **kwargs):
|
def __init_subclass__(cls, /, *args, **kwargs):
|
||||||
|
@ -54,11 +54,6 @@ General-purpose aggregation functions
|
|||||||
ArrayAgg("a_field", order_by="-some_field")
|
ArrayAgg("a_field", order_by="-some_field")
|
||||||
ArrayAgg("a_field", order_by=F("some_field").desc())
|
ArrayAgg("a_field", order_by=F("some_field").desc())
|
||||||
|
|
||||||
.. deprecated:: 5.2
|
|
||||||
|
|
||||||
The ``ordering`` keyword argument is deprecated. Use
|
|
||||||
:attr:`ArrayAgg.order_by` instead.
|
|
||||||
|
|
||||||
``BitAnd``
|
``BitAnd``
|
||||||
----------
|
----------
|
||||||
|
|
||||||
@ -180,11 +175,6 @@ General-purpose aggregation functions
|
|||||||
{'parking': True, 'double_bed': True}
|
{'parking': True, 'double_bed': True}
|
||||||
]}]>
|
]}]>
|
||||||
|
|
||||||
.. deprecated:: 5.2
|
|
||||||
|
|
||||||
The ``ordering`` keyword argument is deprecated. Use
|
|
||||||
:attr:`JSONBAgg.order_by` instead.
|
|
||||||
|
|
||||||
``StringAgg``
|
``StringAgg``
|
||||||
-------------
|
-------------
|
||||||
|
|
||||||
@ -242,11 +232,6 @@ General-purpose aggregation functions
|
|||||||
'headline': 'NASA uses Python', 'publication_names': 'Science News, The Python Journal'
|
'headline': 'NASA uses Python', 'publication_names': 'Science News, The Python Journal'
|
||||||
}]>
|
}]>
|
||||||
|
|
||||||
.. deprecated:: 5.2
|
|
||||||
|
|
||||||
The ``ordering`` keyword argument is deprecated. Use
|
|
||||||
:attr:`StringAgg.order_by` instead.
|
|
||||||
|
|
||||||
Aggregate functions for statistics
|
Aggregate functions for statistics
|
||||||
==================================
|
==================================
|
||||||
|
|
||||||
|
@ -272,3 +272,9 @@ to remove usage of these features.
|
|||||||
* Fallbacks to ``request.user`` and ``request.auser()`` when ``user`` is
|
* Fallbacks to ``request.user`` and ``request.auser()`` when ``user`` is
|
||||||
``None`` in ``django.contrib.auth.login()`` and
|
``None`` in ``django.contrib.auth.login()`` and
|
||||||
``django.contrib.auth.alogin()``, respectively, are removed.
|
``django.contrib.auth.alogin()``, respectively, are removed.
|
||||||
|
|
||||||
|
* The ``ordering`` keyword parameter of the PostgreSQL specific aggregation
|
||||||
|
functions ``django.contrib.postgres.aggregates.ArrayAgg``,
|
||||||
|
``django.contrib.postgres.aggregates.JSONBAgg``, and
|
||||||
|
``django.contrib.postgres.aggregates.StringAgg`` are removed in favor
|
||||||
|
of the ``order_by`` parameter.
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
import warnings
|
|
||||||
|
|
||||||
from django.db import transaction
|
from django.db import transaction
|
||||||
from django.db.models import (
|
from django.db.models import (
|
||||||
CharField,
|
CharField,
|
||||||
@ -17,7 +15,7 @@ from django.db.models.fields.json import KeyTransform
|
|||||||
from django.db.models.functions import Cast, Concat, LPad, Substr
|
from django.db.models.functions import Cast, Concat, LPad, Substr
|
||||||
from django.test.utils import Approximate
|
from django.test.utils import Approximate
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from django.utils.deprecation import RemovedInDjango61Warning, RemovedInDjango70Warning
|
from django.utils.deprecation import RemovedInDjango70Warning
|
||||||
|
|
||||||
from . import PostgreSQLTestCase
|
from . import PostgreSQLTestCase
|
||||||
from .models import AggregateTestModel, HotelReservation, Room, StatTestModel
|
from .models import AggregateTestModel, HotelReservation, Room, StatTestModel
|
||||||
@ -147,46 +145,6 @@ class TestGeneralAggregate(PostgreSQLTestCase):
|
|||||||
)
|
)
|
||||||
self.assertEqual(values, {"aggregation": expected_result})
|
self.assertEqual(values, {"aggregation": expected_result})
|
||||||
|
|
||||||
def test_ordering_warns_of_deprecation(self):
|
|
||||||
msg = "The ordering argument is deprecated. Use order_by instead."
|
|
||||||
with self.assertWarnsMessage(RemovedInDjango61Warning, msg) as ctx:
|
|
||||||
values = AggregateTestModel.objects.aggregate(
|
|
||||||
arrayagg=ArrayAgg("integer_field", ordering=F("integer_field").desc())
|
|
||||||
)
|
|
||||||
self.assertEqual(values, {"arrayagg": [2, 1, 0, 0]})
|
|
||||||
self.assertEqual(ctx.filename, __file__)
|
|
||||||
|
|
||||||
# RemovedInDjango61Warning: Remove this test
|
|
||||||
def test_ordering_and_order_by_causes_error(self):
|
|
||||||
with warnings.catch_warnings(record=True, action="always") as wm:
|
|
||||||
with self.assertRaisesMessage(
|
|
||||||
TypeError,
|
|
||||||
"Cannot specify both order_by and ordering.",
|
|
||||||
):
|
|
||||||
AggregateTestModel.objects.aggregate(
|
|
||||||
stringagg=StringAgg(
|
|
||||||
"char_field",
|
|
||||||
delimiter=Value("'"),
|
|
||||||
order_by="char_field",
|
|
||||||
ordering="char_field",
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
first_warning = wm[0]
|
|
||||||
self.assertEqual(first_warning.category, RemovedInDjango70Warning)
|
|
||||||
self.assertEqual(
|
|
||||||
"The PostgreSQL specific StringAgg function is deprecated. Use "
|
|
||||||
"django.db.models.aggregate.StringAgg instead.",
|
|
||||||
str(first_warning.message),
|
|
||||||
)
|
|
||||||
|
|
||||||
second_warning = wm[1]
|
|
||||||
self.assertEqual(second_warning.category, RemovedInDjango61Warning)
|
|
||||||
self.assertEqual(
|
|
||||||
"The ordering argument is deprecated. Use order_by instead.",
|
|
||||||
str(second_warning.message),
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_array_agg_charfield(self):
|
def test_array_agg_charfield(self):
|
||||||
values = AggregateTestModel.objects.aggregate(arrayagg=ArrayAgg("char_field"))
|
values = AggregateTestModel.objects.aggregate(arrayagg=ArrayAgg("char_field"))
|
||||||
self.assertEqual(values, {"arrayagg": ["Foo1", "Foo2", "Foo4", "Foo3"]})
|
self.assertEqual(values, {"arrayagg": ["Foo1", "Foo2", "Foo4", "Foo3"]})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user