#28446: QuerySet.extra is required for using SQL functions in SELECT clause
-------------------------------------+-------------------------------------
Reporter: chuck- | Owner: nobody
horowitz |
Type: | Status: new
Cleanup/optimization |
Component: Database | Version: 1.11
layer (models, ORM) |
Severity: Normal | Keywords: QuerySet.extra
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
In this case, I have a table that is indexed and queried based on long
integer IP addresses. My QuerySet will ultimately be passed along to a
serializer expecting an IPAddressField compatible item however, which must
be a valid IPv4 string. So I would like SQL that looks like:
{{{
SELECT INET_NTOA(iplong) AS ip, COUNT(*) AS count FROM ipTable GROUP BY
INET_NTOA(iplong) ORDER BY count DESC;
}}}
To do this with ORM and QuerySet.extra() looks like:
{{{
Model.extra(select={'ip':
'INET_NTOA(iplong)'}).values('ip').annotate(count=Count('*')).order_by('-count')
}}}
This is not a problem for my application, but I wanted to pass this along
as requested in the documentation. Additionally, perhaps there is a
better/more idomatic way of doing this (not requiring QuerySet.extra())
that you might be able to point me towards.
Thanks for making a great framework!
Best,
Chuck
--
Ticket URL: <https://code.djangoproject.com/ticket/28446>
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 post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/057.94bdff09be8fff2c52dface85fe6c8d9%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.