#14030: Use F() objects in aggregates(), annotates() and values()
-------------------------------------+-------------------------------------
     Reporter:  delfick              |                    Owner:  jarshwah
         Type:  New feature          |                   Status:  new
    Component:  Database layer       |                  Version:  master
  (models, ORM)                      |               Resolution:
     Severity:  Normal               |             Triage Stage:  Accepted
     Keywords:  aggregate, annotate  |      Needs documentation:  0
    Has patch:  1                    |  Patch needs improvement:  0
  Needs tests:  0                    |                    UI/UX:  0
Easy pickings:  0                    |
-------------------------------------+-------------------------------------
Changes (by jbg):

 * status:  closed => new
 * resolution:  fixed =>


Comment:

 This patch appears to cause a regression in aggregations on DecimalFields.
 The following exception is raised if, for example, a Sum() aggregation on
 the field results in more digits than the field itself can store (a
 perfectly normal situation).

 {{{
 Traceback (most recent call last):
   File "/home/.../django/django/core/handlers/base.py", line 131, in
 get_response
     response = wrapped_callback(request, *callback_args,
 **callback_kwargs)
   File "/usr/lib/python3.4/contextlib.py", line 30, in inner
     return func(*args, **kwds)
   File "/home/.../main/staff.py", line 24, in func
     return view(request, employee, *args, **kwargs)
   File "/home/.../main/staff.py", line 62, in payslip
     ytd_gross_income =
 ytd_payslips.aggregate(total=Sum('gross_income'))['total']
   File "/home/.../django/django/db/models/query.py", line 342, in
 aggregate
     return query.get_aggregation(self.db, kwargs.keys())
   File "/home/.../django/django/db/models/sql/query.py", line 422, in
 get_aggregation
     result = compiler.apply_converters(result, converters)
   File "/home/.../django/django/db/models/sql/compiler.py", line 698, in
 apply_converters
     value = converter(value, self.connection)
   File "/home/.../django/django/db/models/expressions.py", line 258, in
 convert_value
     return backend_utils.typecast_decimal(field.format_number(value))
   File "/home/.../django/django/db/models/fields/__init__.py", line 1556,
 in format_number
     return utils.format_number(value, self.max_digits,
 self.decimal_places)
   File "/home/.../django/django/db/backends/utils.py", line 197, in
 format_number
     return "{0:f}".format(value.quantize(decimal.Decimal(".1") **
 decimal_places, context=context))
   File "/usr/lib/python3.4/decimal.py", line 2580, in quantize
     'quantize result has too many digits for current context')
   File "/usr/lib/python3.4/decimal.py", line 4050, in _raise_error
     raise error(explanation)
 decimal.InvalidOperation: quantize result has too many digits for current
 context
 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/14030#comment:54>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/065.d2d0e2d74f614ad338cf685bc74d776f%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to