Opened 5 years ago

Closed 16 months ago

#16092 closed Bug (wontfix)

QuerySet F() fields should be able to reference .extra() select keys

Reported by: Joshua "jag" Ginsberg <jag@…> Owned by: nobody
Component: Database layer (models, ORM) Version: 1.3
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


from django.db import models
from datetime import datetime

class TestModel(models.Model):
    word = models.CharField(max_length=50)
    timezone_name = models.CharField(max_length=200)
    cutoff = models.DateTimeField()

    class Meta:
        db_table = 'test_model'

obj1 = TestModel.objects.create(word='Foo', timezone_name='America/New_York', cutoff=datetime(2012,1,1,0,0,0))
obj2 = TestModel.objects.create(word='Bar', timezone_name='America/Los_Angeles', cutoff=datetime(2000,1,1,0,0,0))

qs = TestModel.objects.extra(select={'now_in_local_tz': "convert_tz(now(), 'GMT', test_model.timezone_name)"})
qs = qs.filter(cutoff__lte=models.F('now_in_local_tz'))
# FieldError: Cannot resolve keyword 'now_in_local_tz' into field. Choices are: cutoff, id, timezone_name, word

Disclaimer: I wrote the above code without testing it based on a far more complex real-world example - any typos ought not invalidate the bug.

Attachments (1)

16092-testcase.patch (1.8 KB) - added by aaugustin 5 years ago.

Download all attachments as: .zip

Change History (4)

Changed 5 years ago by aaugustin

comment:1 Changed 5 years ago by aaugustin

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

Here is a formal test case based on the example in the original report.

Last edited 5 years ago by aaugustin (previous) (diff)

comment:2 Changed 4 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:3 Changed 16 months ago by timgraham

  • Resolution set to wontfix
  • Status changed from new to closed

I think the new expressions API in Django 1.8 mostly obsoletes this. The plan is to deprecate .extra() when all of its functionality can be achieved in other ways.

Note: See TracTickets for help on using tickets.
Back to Top