[Django] #36693: The "default" argument for ArrayAgg is being ignored

4 views
Skip to first unread message

Django

unread,
Oct 28, 2025, 2:15:17 PM10/28/25
to django-...@googlegroups.com
#36693: The "default" argument for ArrayAgg is being ignored
-------------------------------------+-------------------------------------
Reporter: Bryant Glisson | Type: Bug
Status: new | Component: Database
| layer (models, ORM)
Version: 5.2 | Severity: Normal
Keywords: annotation, array | Triage Stage:
aggregation | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
I am overriding the `get_queryset` method of the admin and returning the
following:

{{{
return
super().get_queryset(request).annotate(sites_list=ArrayAgg('sites__name',
default=Value([]), distinct=True))
}}}

The idea is to return a list of what sites a given record is associated
with. When there are no sites, I'd like it to return an empty list. The
docs suggest that the above should accomplish this, but it's returning
`[None]` instead. I tried replacing `Value([])` with `[]`, but results are
unchanged. If I replace it with `"TEST"`, I get an error "malformed array
literal: 'TEST'", so it is attempting to interact with the argument, but
both of the attempts at providing a list here are ignored. Any idea what
may be causing this?
--
Ticket URL: <https://code.djangoproject.com/ticket/36693>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Oct 28, 2025, 6:30:02 PM10/28/25
to django-...@googlegroups.com
#36693: The "default" argument for ArrayAgg is being ignored
-------------------------------------+-------------------------------------
Reporter: Bryant Glisson | Owner: (none)
Type: Bug | Status: closed
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: annotation, array | Triage Stage:
aggregation | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by ontowhee):

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

Comment:

Hello! Is there a record for your model that has a None value for the
`sites` field? If there are no records for your model, the expected return
value should be an empty list, given the default that you have provided.
However, if there is at least one record, the aggregation is expected to
return a list with some items in it.

There are examples of how the default parameter is used in
[https://github.com/django/django/blob/43933a1dca07047e95ec990d9289d0212668009e/tests/postgres_tests/test_aggregates.py#L116
test_default_argument]. In this test, all the records for
`AggregateTestModel` have been deleted, and the aggregations are returning
the values specified in the default parameter.

In the attempt to reproduce your case, I created one record of the
AggregateTestModel with an integer_field of `None`, and observed that the
aggregation returned a list with one `None` value. Is this similar to what
you are observing in your application?

{{{
AggregateTestModel.objects.create(char_field="hello")
aggregation = ArrayAgg("integer_field", default=[])
expected_result = []
with self.assertNumQueries(1):
values = AggregateTestModel.objects.aggregate(
aggregation=aggregation
)
self.assertEqual(values, {"aggregation": expected_result})
}}}

Closing this ticket because the default parameter is working as expected.
If this assessment is incorrect, feel free to reopen and provide more
details on reproducing the bug.

If you have further questions on how to use Django, feel free to ask for
help from a friendly community member on ​[https://chat.djangoproject.com/
Discord] or the [https://forum.djangoproject.com/ Django forum]!
--
Ticket URL: <https://code.djangoproject.com/ticket/36693#comment:1>

Django

unread,
Oct 28, 2025, 6:38:04 PM10/28/25
to django-...@googlegroups.com
#36693: The "default" argument for ArrayAgg is being ignored
-------------------------------------+-------------------------------------
Reporter: Bryant Glisson | Owner: (none)
Type: Bug | Status: closed
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: annotation, array | Triage Stage:
aggregation | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Simon Charette):

ontowee's assessment is on point.

You haven't provided your model definition so it's hard to tell for sure
but all signs point at your aggregate function targeting a multi-valued
nullable relationship (`sites`) which means that it will include `NULL`
when there are no matches.

If you want to exclude them from the targeted set you should pass
`filter=~Q(sites=None)` to your `ArrayAgg` which will default to `NULL` on
an empty set and use the provided `default`.
--
Ticket URL: <https://code.djangoproject.com/ticket/36693#comment:2>
Reply all
Reply to author
Forward
0 new messages