#28446: QuerySet.extra is required for using SQL functions in SELECT clause
-------------------------------------+-------------------------------------
Reporter: Chuck Horowitz | Owner: nobody
Type: | Status: closed
Cleanup/optimization |
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: QuerySet.extra | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):
* status: new => closed
* resolution: => invalid
Comment:
Hello Chuck,
You should be able to use `Func`
[https://docs.djangoproject.com/en/1.11/ref/models/expressions/#func-
expressions for this purpose].
{{{#!python
from django.db import models
from django.db.models import Func, F
Model.objects.annotate(
ip=Func(F('iplong'), function='INET_NTOA',
output_field=models.CharField(max_length=15)),
).values('ip').annotate(count=Count('*')).order_by('-count')
}}}
You can even define your own `Func` subclass and use it instead.
{{{#!python
class INetNTOA(Func):
function = 'INET_NTOA'
output_field = models.CharField(max_length=15)
Model.objects.annotate(
ip=INetNTOA(F('iplong')),
).values('ip').annotate(count=Count('*')).order_by('-count')
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28446#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 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/072.4805b2b18239e82ec45a65bf6104f9aa%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.