Opened 6 years ago

Closed 23 months ago

#10466 closed Cleanup/optimization (duplicate)

Document that functions in QuerySets' parameters are eagerly evaluated.

Reported by: liangent Owned by: nobody
Component: Documentation Version: master
Severity: Normal Keywords:
Cc: liangent@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

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)

comment:1 Changed 6 years ago by mtredinnick

  • Component changed from Database layer (models, ORM) to Documentation
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to fixed
  • Status changed from new to closed
  • Summary changed from functions as QuerySets' parameters are not lazy to Docuemtn that functions in QuerySets' parameters are eagerly evaluated.
  • Triage Stage changed from Unreviewed to 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. :-)

Changing the title to reflect how we'll resolve this.

comment:2 Changed 6 years ago by liangent

  • Summary changed from Docuemtn that functions in QuerySets' parameters are eagerly evaluated. to Document that functions in QuerySets' parameters are eagerly evaluated.

comment:3 Changed 6 years ago by mtredinnick

  • Resolution fixed deleted
  • Status changed from closed to reopened

Not quite sure why I closed this, since I didn't update the documentation.

comment:4 Changed 5 years ago by anonymous

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)

comment:5 Changed 4 years ago by SmileyChris

  • Severity set to Normal
  • Type set to Cleanup/optimization

comment:6 Changed 3 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:7 Changed 3 years ago by aaugustin

  • Easy pickings unset

Change Easy pickings from NULL to False.

comment:8 Changed 2 years ago by aaugustin

  • Status changed from reopened to new

comment:9 Changed 23 months ago by bmispelon

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

I'm closing this as a duplicate of #20241.

Even though this ticket is much older, the other one has some explanations as to why fixing this bug is tricky.

Personally, I think this feature should be removed since it doesn't the only thing it does (from what I understand) is basically save the user from typing a pair of parentheses.

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