#32449: Issues with RawSQL annotations dependencies and count()
-------------------------------------+-------------------------------------
Reporter: João Carneiro Haas | Owner: nobody
Type: New feature | Status: closed
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: annotate, count, | Triage Stage:
RawSQL, select_related, JOIN | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):
* status: new => closed
* type: Uncategorized => New feature
* resolution: => wontfix
Comment:
Thanks for this ticket, however `RawSQL()` is the last resort and I don't
think we would like to add any extra API to it. You can do the same by
creating a custom function (preferable):
{{{
class CustomName(Func):
function = 'custom_name'
output_field = CharField()
Account.objects.select_related('org').annotate(org_name=CustomName('org__name')).count()
}}}
or by using `QuerySet.extra()` (which also supports `tables`):
{{{
Account.objects.select_related('org').extra(select={'org_name': 'SELECT
custom_name(accounts_org.name)'}).count()
}}}
or by specifying the full subquery in `RawSQL()`:
{{{
Account.objects.select_related("org").annotate(org_name=RawSQL(
'SELECT custom_name(accounts_org.name) '
'FROM accounts_org '
'WHERE accounts_account.org_id = ccounts_org.id
)).count()
}}}
I treated this as a request for a new API, because resolving #28477 will
strip unused annotation, so crash is a duplicate of #28477.
--
Ticket URL: <https://code.djangoproject.com/ticket/32449#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/066.bea7d7873a7812c383b33eb553cd2c90%40djangoproject.com.