Opened 4 hours ago

Closed 4 minutes ago

#36693 closed Bug (invalid)

The "default" argument for ArrayAgg is being ignored

Reported by: Bryant Glisson Owned by:
Component: Database layer (models, ORM) Version: 5.2
Severity: Normal Keywords: annotation, array aggregation
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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?

Change History (1)

comment:1 by ontowhee, 4 minutes ago

Resolution: invalid
Status: newclosed

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 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 ​Discord or the Django forum!

Note: See TracTickets for help on using tickets.
Back to Top