2👍
✅
Django does not allow you to do this using the aggregate
method on queryset objects, as this is more complex than Django is trying to aim for. This may change in the near future with the improved query APIs, allowing for custom aggregate methods.
Until then, you can do this using Django’s raw
method on querysets, which allows you to execute arbitrary SQL. Keep in mind that this is not secured by Django, so you need to watch out for any possible issues in your SQL.
Source:stackexchange.com