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

Refs #28643 -- Changed StdDev() to use NumericOutputFieldMixin.

Keeps precision instead of forcing DecimalField to FloatField.
This commit is contained in:
Nick Pope 2018-12-19 23:03:42 +00:00 committed by Tim Graham
parent c690afb873
commit e85afa5943
4 changed files with 9 additions and 8 deletions

View File

@ -141,9 +141,8 @@ class Min(Aggregate):
name = 'Min'
class StdDev(Aggregate):
class StdDev(NumericOutputFieldMixin, Aggregate):
name = 'StdDev'
output_field = FloatField()
def __init__(self, expression, sample=False, **extra):
self.function = 'STDDEV_SAMP' if sample else 'STDDEV_POP'

View File

@ -3387,12 +3387,13 @@ by the aggregate.
``StdDev``
~~~~~~~~~~
.. class:: StdDev(expression, sample=False, filter=None, **extra)
.. class:: StdDev(expression, output_field=None, sample=False, filter=None, **extra)
Returns the standard deviation of the data in the provided expression.
* Default alias: ``<field>__stddev``
* Return type: ``float``
* Return type: ``float`` if input is ``int``, otherwise same as input
field, or ``output_field`` if supplied
Has one optional argument:

View File

@ -493,8 +493,9 @@ Miscellaneous
* :djadmin:`runserver` no longer supports `pyinotify` (replaced by Watchman).
* The :class:`~django.db.models.Avg` aggregate function now returns a
``Decimal`` instead of a ``float`` when the input is ``Decimal``.
* The :class:`~django.db.models.Avg` and :class:`~django.db.models.StdDev`
aggregate functions now return a ``Decimal`` instead of a ``float`` when the
input is ``Decimal``.
.. _deprecated-features-2.2:

View File

@ -1130,7 +1130,7 @@ class AggregationTests(TestCase):
self.assertEqual(
Book.objects.aggregate(StdDev('price')),
{'price__stddev': Approximate(24.16, 2)}
{'price__stddev': Approximate(Decimal('24.16'), 2)}
)
self.assertEqual(
@ -1145,7 +1145,7 @@ class AggregationTests(TestCase):
self.assertEqual(
Book.objects.aggregate(StdDev('price', sample=True)),
{'price__stddev': Approximate(26.46, 1)}
{'price__stddev': Approximate(Decimal('26.46'), 1)}
)
self.assertEqual(