Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#24486 closed Bug (fixed)

UnboundLocalError in db.models.expressions

Reported by: Michael Angeletti Owned by: Josh Smeaton
Component: Database layer (models, ORM) Version: 1.8beta2
Severity: Release blocker Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

After filing #24485, I stumbled into a bug that is somewhat related, in the sense of its relationship to output_field and DurationField support in general. An UnboundLocalError occurs @ https://github.com/django/django/blob/ceaf31adfff3801f1092a215f73704e15a70e90c/django/db/models/expressions.py#L401.

I discovered this while running the following:

Ticket.objects.filter(
    active_at__lte=now,
    active_at__gt=now - models.F('duration')
)

Using the following model:

class Ticket(models.Model):
    # ... other fields omitted
    active_at = models.DateTimeField()
    duration = models.DurationField()

I've categorized this as a release blocker, because it prohibits the use of DurationField with F.

Change History (8)

comment:1 by Josh Smeaton, 10 years ago

I can see the problem in the code, but I can't replicate the error for some reason.

In [16]: Ticket.objects.filter(
    active_at__lte=now,
    active_at__gt=now - models.F('duration')
)
Out[16]: [<Ticket: Ticket object>]

Is there something else in your query that helps to trigger this condition? Though I doubt it'll make a difference, which database are you using to test?

comment:2 by Michael Angeletti, 10 years ago

@jarshwa I'm using sqlite3 (local dev). I don't think there is anything in the code that could result in this difference, but in the morning I will create a new app and try to replicate it in isolation. Perhaps, there is something else in my app that is having some sort of affect.

comment:3 by Josh Smeaton, 10 years ago

Owner: changed from nobody to Josh Smeaton
Status: newassigned
Triage Stage: UnreviewedAccepted
Version: master1.8beta2

@yoyoma it *is* triggered by databases where: not connection.features.has_native_duration_field such as sqlite. I've got enough to investigate, thanks for the report.

comment:4 by Josh Smeaton, 10 years ago

Has patch: set
Triage Stage: AcceptedReady for checkin

comment:5 by Michael Angeletti, 10 years ago

@jarshwah thanks. I've just donated $25 to the Django Fellowship program as a small thanks for your help, as well as the help of @timgraham who helped me with a few tickets recently.

comment:6 by Tim Graham <timograham@…>, 10 years ago

Resolution: fixed
Status: assignedclosed

In ff2aa401:

Fixed #24486 -- Fixed error with datetime and DurationField arithmetic

comment:7 by Tim Graham <timograham@…>, 10 years ago

In 34e09e5:

[1.8.x] Fixed #24486 -- Fixed error with datetime and DurationField arithmetic

Backport of ff2aa4019259947734c4791d6afc9f5405d901d0 from master

comment:8 by Michael Angeletti, 10 years ago

For posterity, the patch addressed the issue fully, and sqlite3 is also supported. Thanks again, @timgraham and @jarshwah.

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