[Django] #32359: Should we define __round__ on Expression?

11 views
Skip to first unread message

Django

unread,
Jan 16, 2021, 6:48:17 PM1/16/21
to django-...@googlegroups.com
#32359: Should we define __round__ on Expression?
-------------------------------------+-------------------------------------
Reporter: Willem | Owner: nobody
Van Onsem |
Type: New | Status: new
feature |
Component: Database | Version: master
layer (models, ORM) |
Severity: Normal | Keywords: aggregates
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Rounding values can be done with:


{{{
from django.db.models.functions import Round

Round(Count('related_model'))
}}}

But we could also implement the __round__ function on the Expression
class, and thus use Python's round(...) function, then it thus no longer
requires an import:

{{{
round(Count('related_model'))
}}}

A potential problem with this is that people might expect that this will
itself return a number-like object, and not an expression.

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

Django

unread,
Jan 16, 2021, 8:50:05 PM1/16/21
to django-...@googlegroups.com
#32359: Should we define __round__ on Expression?
-------------------------------------+-------------------------------------
Reporter: Willem Van Onsem | Owner: nobody
Type: New feature | Status: closed
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: aggregates | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

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


Comment:

Thanks for the suggestion but I think that sticking to the explicit import
makes for a more cohesive expression interface.

I don't think that avoiding an import is a good enough justification to
support two ways of achieving a similar result even if we were to adopt
this pattern for other ''magic'' functions (e.g. `__abs__` ->
`django.db.models.functions.Abs`).

--
Ticket URL: <https://code.djangoproject.com/ticket/32359#comment:1>

Reply all
Reply to author
Forward
0 new messages