diff options
Diffstat (limited to 'bitbake/lib/toaster/toastergui/templatetags/queryset_to_list_filter.py')
-rw-r--r-- | bitbake/lib/toaster/toastergui/templatetags/queryset_to_list_filter.py | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/bitbake/lib/toaster/toastergui/templatetags/queryset_to_list_filter.py b/bitbake/lib/toaster/toastergui/templatetags/queryset_to_list_filter.py new file mode 100644 index 0000000000..dfc094b591 --- /dev/null +++ b/bitbake/lib/toaster/toastergui/templatetags/queryset_to_list_filter.py | |||
@@ -0,0 +1,26 @@ | |||
1 | from django import template | ||
2 | import json | ||
3 | |||
4 | register = template.Library() | ||
5 | |||
6 | def queryset_to_list(queryset, fields): | ||
7 | """ | ||
8 | Convert a queryset to a list; fields can be set to a comma-separated | ||
9 | string of fields for each record included in the resulting list; if | ||
10 | omitted, all fields are included for each record, e.g. | ||
11 | |||
12 | {{ queryset | queryset_to_list:"id,name" }} | ||
13 | |||
14 | will return a list like | ||
15 | |||
16 | [{'id': 1, 'name': 'foo'}, ...] | ||
17 | |||
18 | (providing queryset has id and name fields) | ||
19 | """ | ||
20 | if fields: | ||
21 | fields_list = [field.strip() for field in fields.split(',')] | ||
22 | return list(queryset.values(*fields_list)) | ||
23 | else: | ||
24 | return list(queryset.values()) | ||
25 | |||
26 | register.filter('queryset_to_list', queryset_to_list) | ||