1 | Index: template/defaultfilters.py
|
---|
2 | ===================================================================
|
---|
3 | --- template/defaultfilters.py (revision 16332)
|
---|
4 | +++ template/defaultfilters.py (working copy)
|
---|
5 | @@ -15,6 +15,7 @@
|
---|
6 | from django.utils.html import conditional_escape
|
---|
7 | from django.utils.safestring import mark_safe, SafeData
|
---|
8 | from django.utils.translation import ugettext, ungettext
|
---|
9 | +from django.db.models.query import QuerySet
|
---|
10 |
|
---|
11 | register = Library()
|
---|
12 |
|
---|
13 | @@ -529,10 +530,24 @@
|
---|
14 | length_is.is_safe = False
|
---|
15 |
|
---|
16 | def random(value):
|
---|
17 | - """Returns a random item from the list."""
|
---|
18 | - return random_module.choice(value)
|
---|
19 | + """Returns a random item from a list or QuerySet"""
|
---|
20 | + if not isinstance(value, QuerySet):
|
---|
21 | + return random_module.choice(value)
|
---|
22 | + else:
|
---|
23 | + if len(value) == 0:
|
---|
24 | + return []
|
---|
25 | + return value.order_by('?')[0]
|
---|
26 | random.is_safe = True
|
---|
27 |
|
---|
28 | +def shuffle(value):
|
---|
29 | + """Returns a randomized list or QuerySet"""
|
---|
30 | + if not isinstance(value, QuerySet):
|
---|
31 | + random_module.shuffle(value)
|
---|
32 | + return value
|
---|
33 | + else:
|
---|
34 | + return value.order_by('?')
|
---|
35 | +shuffle.is_safe = True
|
---|
36 | +
|
---|
37 | def slice_(value, arg):
|
---|
38 | """
|
---|
39 | Returns a slice of the list.
|
---|
40 | @@ -914,6 +929,7 @@
|
---|
41 | register.filter(pprint)
|
---|
42 | register.filter(removetags)
|
---|
43 | register.filter(random)
|
---|
44 | +register.filter(shuffle)
|
---|
45 | register.filter(rjust)
|
---|
46 | register.filter(safe)
|
---|
47 | register.filter(safeseq)
|
---|