Code

Ticket #4620: queryset-lambda.diff

File queryset-lambda.diff, 2.9 KB (added by Bill Fenner <fenner@…>, 7 years ago)

Adds a function to the query set iterator and model*choicefield

Line 
1--- django/newforms/models.py.orig      2007-06-19 00:00:29.000000000 -0400
2+++ django/newforms/models.py   2007-06-19 00:14:02.000000000 -0400
3@@ -107,14 +107,14 @@
4     return type('FormForFields', (BaseForm,), {'base_fields': fields})
5 
6 class QuerySetIterator(object):
7-    def __init__(self, queryset, empty_label, cache_choices):
8-        self.queryset, self.empty_label, self.cache_choices = queryset, empty_label, cache_choices
9+    def __init__(self, queryset, empty_label, cache_choices, func):
10+        self.queryset, self.empty_label, self.cache_choices, self.func = queryset, empty_label, cache_choices, func
11 
12     def __iter__(self):
13         if self.empty_label is not None:
14             yield (u"", self.empty_label)
15         for obj in self.queryset:
16-            yield (obj._get_pk_val(), str(obj))
17+            yield (obj._get_pk_val(), self.func(obj))
18         # Clear the QuerySet cache if required.
19         if not self.cache_choices:
20             self.queryset._result_cache = None
21@@ -124,10 +124,11 @@
22     # This class is a subclass of ChoiceField for purity, but it doesn't
23     # actually use any of ChoiceField's implementation.
24     def __init__(self, queryset, empty_label=u"---------", cache_choices=False,
25-            required=True, widget=Select, label=None, initial=None, help_text=None):
26+            required=True, widget=Select, label=None, initial=None, help_text=None, func = lambda obj: str(obj)):
27         self.queryset = queryset
28         self.empty_label = empty_label
29         self.cache_choices = cache_choices
30+        self.func = func
31         # Call Field instead of ChoiceField __init__() because we don't need
32         # ChoiceField.__init__().
33         Field.__init__(self, required, widget, label, initial, help_text)
34@@ -144,7 +145,7 @@
35         # *each* time _get_choices() is called (and, thus, each time
36         # self.choices is accessed) so that we can ensure the QuerySet has not
37         # been consumed.
38-        return QuerySetIterator(self.queryset, self.empty_label, self.cache_choices)
39+        return QuerySetIterator(self.queryset, self.empty_label, self.cache_choices, self.func)
40 
41     def _set_choices(self, value):
42         # This method is copied from ChoiceField._set_choices(). It's necessary
43@@ -168,9 +169,9 @@
44     "A MultipleChoiceField whose choices are a model QuerySet."
45     hidden_widget = MultipleHiddenInput
46     def __init__(self, queryset, cache_choices=False, required=True,
47-            widget=SelectMultiple, label=None, initial=None, help_text=None):
48+            widget=SelectMultiple, label=None, initial=None, help_text=None, func = lambda obj: str(obj)):
49         super(ModelMultipleChoiceField, self).__init__(queryset, None, cache_choices,
50-            required, widget, label, initial, help_text)
51+            required, widget, label, initial, help_text, func)
52 
53     def clean(self, value):
54         if self.required and not value: