Commit 2d76b61d authored by Tim Graham's avatar Tim Graham
Browse files

Fixed #24649 -- Allowed using Avg aggregate on non-numeric field types.

parent 26996e2d
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -160,6 +160,9 @@ class BaseDatabaseFeatures(object):
    # Support for the DISTINCT ON clause
    can_distinct_on_fields = False

    # Can the backend use an Avg aggregate on DurationField?
    can_avg_on_durationfield = True

    # Does the backend decide to commit before SAVEPOINT statements
    # when autocommit is disabled? http://bugs.python.org/issue8145#msg109965
    autocommits_when_autocommit_is_off = False
+1 −0
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@ class DatabaseFeatures(BaseDatabaseFeatures):
    uppercases_column_names = True
    # select for update with limit can be achieved on Oracle, but not with the current backend.
    supports_select_for_update_with_limit = False
    can_avg_on_durationfield = False  # Pending implementation (#24699).

    def introspected_boolean_field_type(self, field=None, created_separately=False):
        """
+2 −6
Original line number Diff line number Diff line
@@ -75,12 +75,8 @@ class Avg(Aggregate):
    name = 'Avg'

    def __init__(self, expression, **extra):
        super(Avg, self).__init__(expression, output_field=FloatField(), **extra)

    def convert_value(self, value, expression, connection, context):
        if value is None:
            return value
        return float(value)
        output_field = extra.pop('output_field', FloatField())
        super(Avg, self).__init__(expression, output_field=output_field, **extra)


class Count(Aggregate):
+10 −3
Original line number Diff line number Diff line
@@ -2802,12 +2802,19 @@ by the aggregate.
Avg
~~~

.. class:: Avg(expression, output_field=None, **extra)
.. class:: Avg(expression, output_field=FloatField(), **extra)

    Returns the mean value of the given expression, which must be numeric.
    Returns the mean value of the given expression, which must be numeric
    unless you specify a different ``output_field``.

    * Default alias: ``<field>__avg``
    * Return type: ``float``
    * Return type: ``float`` (or the type of whatever ``output_field`` is
      specified)

    .. versionchanged:: 1.9

        The ``output_field`` parameter was added to allow aggregating over
        non-numeric columns, such as ``DurationField``.

Count
~~~~~
+4 −0
Original line number Diff line number Diff line
@@ -200,6 +200,10 @@ Models
  (such as :lookup:`exact`, :lookup:`gt`, :lookup:`lt`, etc.). For example:
  ``Entry.objects.filter(pub_date__month__gt=6)``.

* You can specify the ``output_field`` parameter of the
  :class:`~django.db.models.Avg` aggregate in order to aggregate over
  non-numeric columns, such as ``DurationField``.

CSRF
^^^^

Loading