Code

Opened 3 years ago

Last modified 3 years ago

#16092 new Bug

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

Description

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 3 years ago.

Download all attachments as: .zip

Change History (3)

Changed 3 years ago by aaugustin

comment:1 Changed 3 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 3 years ago by aaugustin (previous) (diff)

comment:2 Changed 2 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as new
The owner will be changed from nobody to anonymous. Next status will be 'assigned'
as The resolution will be set. Next status will be 'closed'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.