#20241: QuerySets with callable filter args have said args evaluated only once
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
christopher.m.pike@… | Status: new
Type: Bug | Version: 1.5
Component: Database layer | Resolution:
(models, ORM) | Triage Stage: Accepted
Severity: Normal | Needs documentation: 0
Keywords: | Patch needs improvement: 0
Has patch: 0 | UI/UX: 0
Needs tests: 0 |
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by bmispelon):
* severity: Release blocker => Normal
* cc: bmispelon@… (added)
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
* stage: Unreviewed => Accepted
Comment:
I can reproduce this issue.
Using the following code, you can see that the callable arguments are
indeed only evaluated once (when the urls.py file is read for the first
time):
{{{
#!python
from django.conf.urls import patterns, include, url
from django.views.generic import ListView
from django.utils import timezone
from .models import Poll
def foo():
print('called')
return timezone.now()
urlpatterns = patterns('',
url('^$', ListView.as_view(
queryset=Poll.objects.filter(pub_date__lte=foo),
)),
)
}}}
However, unlike what it described on the linked SO post, I find that the
issue is present on master, 1.5, and 1.4 so I don't think it's a
regression.
This feature (passing callable arguments to `filter`) is only mentionned
in the 5th part of the tutorial and it's not documented anywhere else (see
#11629).
I'm not sure if this is a bug or intended behavior.
I the issue here lies with the documentation and I would suggest re-
writing that part of the tutorial.
With normal pyhton code, anything sitting at the top-level of a module is
evaluated when the file is read.
In the case of querysets, they do magic things to delay their evaluation
which makes the code kind-of work but I don't think it's a good habit to
teach.
--
Ticket URL: <https://code.djangoproject.com/ticket/20241#comment:1>
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].
For more options, visit https://groups.google.com/groups/opt_out.