#28086: Support Resolving F Expression in Custom DB Function
-------------------------------------+-------------------------------------
Reporter: Charlie | Owner: nobody
McBride |
Type: New | Status: new
feature |
Component: Database | Version: 1.10
layer (models, ORM) |
Severity: Normal | Keywords: F, Func
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
I am trying to write a custom postgresql function that will coerce
datetimes to a specified timezone inside of a queryset. In failing to do
so, I noticed that it is not currently possible to resolve kwargs that are
F expressions in a custom `Func`. My first pass at the db function looks
like this:
{{{
from django.db.models.expressions import Func
class DateTimeInTimezone(Func):
template="%(expressions)s AT TIME ZONE %(tz_info)s"
}}}
This function works in the simple case where I pass a timezone string into
the function directly like so:
{{{
MyModel.objects.filter(timefield__gte=DateTimeInTimezone(Now(),
tz_info='EST'))
}}}
However it doesn't work in the more complex case, where the timezone is
defined on some field on the model. Consider the following contrived
example:
{{{
class User(models.Model):
time_zone = models.CharField()
class Meeting(models.Model):
users = models.ManyToManyField(User)
start_time = models.DateTimeField() # in UTC
end_time = models.DateTimeField() # in UTC
}}}
To answer the question "What users will be in a meeting at 12pm local time
today?", I'd need some variation of this queryset:
{{{
noon_utc = ...
User.objects.filter(
meetings__start_time__lte=DateTimeInTimezone(noon_utc,
tz_info=F('time_zone')),
meetings__end_time__gt=DateTimeInTimezone(noon_utc,
tz_info=F('time_zone'))
)
}}}
As currently written, however, `DateTimeInTimezone` will simply inject the
string `F('time_zone')` into the sql rather than resolve the expression to
the row value.
In the source for `Func` it's evident that the `expressions` are checked
for `resolve_expression` attributes and resolved if possible, while any
`extra` attributes (i.e. kwargs) are not. Is it possible to add support
for evaluating `extra` in `Func` in the same manner as `expressions` to
support dynamic values via F expressions? In Django 1.10 the code in
question is in `django/db/models/expressions.py:472 - 551`.
--
Ticket URL: <https://code.djangoproject.com/ticket/28086>
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/053.8f88a5d4d689fa566c4e1174d5962692%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.