﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
28086	Support Resolving F Expression in Custom DB Function	Charlie McBride	nobody	"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`."	New feature	closed	Database layer (models, ORM)	1.10	Normal	invalid	F, Func		Unreviewed	0	0	0	0	0	0
