Return a dictionary containing the calculations (aggregation) over the current queryset. If args is present the expression is passed as a kwarg using the Aggregate object's default alias.
(self, *args, **kwargs)
| 613 | yield item |
| 614 | |
| 615 | def aggregate(self, *args, **kwargs): |
| 616 | """ |
| 617 | Return a dictionary containing the calculations (aggregation) |
| 618 | over the current queryset. |
| 619 | |
| 620 | If args is present the expression is passed as a kwarg using |
| 621 | the Aggregate object's default alias. |
| 622 | """ |
| 623 | if self.query.distinct_fields: |
| 624 | raise NotImplementedError("aggregate() + distinct(fields) not implemented.") |
| 625 | self._validate_values_are_expressions( |
| 626 | (*args, *kwargs.values()), method_name="aggregate" |
| 627 | ) |
| 628 | for arg in args: |
| 629 | # The default_alias property raises TypeError if default_alias |
| 630 | # can't be set automatically or AttributeError if it isn't an |
| 631 | # attribute. |
| 632 | try: |
| 633 | arg.default_alias |
| 634 | except (AttributeError, TypeError): |
| 635 | raise TypeError("Complex aggregates require an alias") |
| 636 | kwargs[arg.default_alias] = arg |
| 637 | |
| 638 | return self.query.chain().get_aggregation(self.db, kwargs) |
| 639 | |
| 640 | async def aaggregate(self, *args, **kwargs): |
| 641 | return await sync_to_async(self.aggregate)(*args, **kwargs) |