random filter should be smarter about querysets
|Reported by:||Idan Gazit||Owned by:||nobody|
|Cc:||Triage Stage:||Design decision needed|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
Apologies in advance if my terminology isn't quite right, I'm new to all of this...
Right now, the random filter's implementation naively chooses from the supplied list. In the case of querysets, this might be wildly inefficient -- converting the queryset into a list causes evaluation of the entire queryset, which might be fairly large. Ideally, the desired outcome is that random will simply limit the queryset to one of its elements (or None if the queryset is empty).
This patch should leave existing behavior intact for everything except querysets.
Patch + documentation attached. Be gentle and tell me what to do next if something is lacking. Particularly: I think I correctly did the cross-linking to the docs about limiting querysets correctly (in the templates documentation for the random filter) -- but would appreciate a check from somebody else.
Due credit for this goes to bram_ and elliott on #django; I came in there to ask about "how best to choose a random element" and this cropped up. The code for the filter is bram's from pastebin. The documentation and diffwork is mine. :)
Change History (5)
comment:1 Changed 9 years ago by
|Triage Stage:||Unreviewed → Design decision needed|