Description

if i write code like this:

qs = MyModel.objects.filter(dt__lte=datetime.datetime.now)

and execute it at time A, then save qs somewhere.

later, at time B, i call for obj in qs to get the object list. as the documentation says, database query is executed at time B. however, it seems that the value of dt__lte was fetched at time A (ie. datetime.datetime.now was evaluated at time A), and i think datetime.datetime.now should be evaluated at time B (if i wrote

qs = MyModel.objects.filter(dt__lte=datetime.datetime.now())

, it should be evaluated at time A). it seems to go against "QuerySets are lazy", doesn't it?

Change History (9)

functions as QuerySets' parameters are not lazy →
Docuemtn that functions in QuerySets' parameters are eagerly evaluated.

Triage Stage:

Unreviewed →
Accepted

I'm going to call this one invalid, but it does need better documentation.

The queryset's evaluation against the database is lazy. The parameter evaluation (unless it's a database query) is done when you create the queryset. It's fairly complicated to arrange for this to be otherwise in all cases and rather than mess around with when it does and doesn't work, we always have eager parameter evaluation.

I will add some documentation, but the behaviour isn't going to change. Sorry about that. :-)

This has bitten me before as well. You might also want to add that if you pass that QuerySet as a parameter to a view in your url config, the QuerySet will have the datetime of server start, since it is only evaluated once.

And also add a way to work around this limitation? (for both the original problem and my url conf problem)