Browse Source

Refs #28459 -- Improved performance of loading DurationField on SQLite and MySQL.

Sergey Fedoseev 7 years ago
parent
commit
ae1baa7d1d
2 changed files with 13 additions and 4 deletions
  1. 1 4
      django/db/backends/base/operations.py
  2. 12 0
      django/db/models/aggregates.py

+ 1 - 4
django/db/backends/base/operations.py

@@ -7,7 +7,6 @@ from django.core.exceptions import ImproperlyConfigured
 from django.db import NotSupportedError, transaction
 from django.db.backends import utils
 from django.utils import timezone
-from django.utils.dateparse import parse_duration
 from django.utils.encoding import force_text
 
 
@@ -568,9 +567,7 @@ class BaseDatabaseOperations:
 
     def convert_durationfield_value(self, value, expression, connection):
         if value is not None:
-            value = str(decimal.Decimal(value) / decimal.Decimal(1000000))
-            value = parse_duration(value)
-        return value
+            return datetime.timedelta(0, 0, value)
 
     def check_expression_support(self, expression):
         """

+ 12 - 0
django/db/models/aggregates.py

@@ -94,6 +94,12 @@ class Avg(Aggregate):
             return FloatField()
         return super()._resolve_output_field()
 
+    def as_mysql(self, compiler, connection):
+        sql, params = super().as_sql(compiler, connection)
+        if self.output_field.get_internal_type() == 'DurationField':
+            sql = 'CAST(%s as SIGNED)' % sql
+        return sql, params
+
     def as_oracle(self, compiler, connection):
         if self.output_field.get_internal_type() == 'DurationField':
             expression = self.get_source_expressions()[0]
@@ -153,6 +159,12 @@ class Sum(Aggregate):
     function = 'SUM'
     name = 'Sum'
 
+    def as_mysql(self, compiler, connection):
+        sql, params = super().as_sql(compiler, connection)
+        if self.output_field.get_internal_type() == 'DurationField':
+            sql = 'CAST(%s as SIGNED)' % sql
+        return sql, params
+
     def as_oracle(self, compiler, connection):
         if self.output_field.get_internal_type() == 'DurationField':
             expression = self.get_source_expressions()[0]