Docs and implementation don't match regading return values of certain QuerySet aggregate functions
|Reported by:||Paul McMillan||Owned by:||nobody|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
The aggregation docs specify that
Sum return a value of the same type as the field on which the function was called. This means that if I use
Max() on a
PositiveIntegerField, I expect to get back a Python
I know that a non-null
PositiveIntegerField can't contain None - setting and saving it throws an IntegrityError.
However, when I use the
Max() aggregate on a PositiveIntegerField in a model with no rows, I get back
None. This behavior occurs in both sqlite and PostgreSQL.
Since this behavior has (presumably) been around since aggregates were introduced, this must be a bug in the docs, and not in Django, even though it is counterintuitive and requires boilerplate code to make absolutely certain that the value is not
None before depending on the typing.
If it were up to me, I would propose changing the return value to be in line with the docs, but that wouldn't be backwards compatible.
Change History (10)
comment:1 Changed 6 years ago by
|Patch needs improvement:||unset|
comment:7 Changed 6 years ago by
|Summary:||Aggregates docs are misleading (or there is a bug in the aggregate code) → Docs and implementation don't match regading return values of certain QuerySet aggregate functions|