Ticket #4620: queryset-lambda.diff
File queryset-lambda.diff, 2.9 KB (added by , 17 years ago) |
---|
-
django/newforms/models.py
old new 107 107 return type('FormForFields', (BaseForm,), {'base_fields': fields}) 108 108 109 109 class QuerySetIterator(object): 110 def __init__(self, queryset, empty_label, cache_choices ):111 self.queryset, self.empty_label, self.cache_choices = queryset, empty_label, cache_choices110 def __init__(self, queryset, empty_label, cache_choices, func): 111 self.queryset, self.empty_label, self.cache_choices, self.func = queryset, empty_label, cache_choices, func 112 112 113 113 def __iter__(self): 114 114 if self.empty_label is not None: 115 115 yield (u"", self.empty_label) 116 116 for obj in self.queryset: 117 yield (obj._get_pk_val(), s tr(obj))117 yield (obj._get_pk_val(), self.func(obj)) 118 118 # Clear the QuerySet cache if required. 119 119 if not self.cache_choices: 120 120 self.queryset._result_cache = None … … 124 124 # This class is a subclass of ChoiceField for purity, but it doesn't 125 125 # actually use any of ChoiceField's implementation. 126 126 def __init__(self, queryset, empty_label=u"---------", cache_choices=False, 127 required=True, widget=Select, label=None, initial=None, help_text=None ):127 required=True, widget=Select, label=None, initial=None, help_text=None, func = lambda obj: str(obj)): 128 128 self.queryset = queryset 129 129 self.empty_label = empty_label 130 130 self.cache_choices = cache_choices 131 self.func = func 131 132 # Call Field instead of ChoiceField __init__() because we don't need 132 133 # ChoiceField.__init__(). 133 134 Field.__init__(self, required, widget, label, initial, help_text) … … 144 145 # *each* time _get_choices() is called (and, thus, each time 145 146 # self.choices is accessed) so that we can ensure the QuerySet has not 146 147 # been consumed. 147 return QuerySetIterator(self.queryset, self.empty_label, self.cache_choices )148 return QuerySetIterator(self.queryset, self.empty_label, self.cache_choices, self.func) 148 149 149 150 def _set_choices(self, value): 150 151 # This method is copied from ChoiceField._set_choices(). It's necessary … … 168 169 "A MultipleChoiceField whose choices are a model QuerySet." 169 170 hidden_widget = MultipleHiddenInput 170 171 def __init__(self, queryset, cache_choices=False, required=True, 171 widget=SelectMultiple, label=None, initial=None, help_text=None ):172 widget=SelectMultiple, label=None, initial=None, help_text=None, func = lambda obj: str(obj)): 172 173 super(ModelMultipleChoiceField, self).__init__(queryset, None, cache_choices, 173 required, widget, label, initial, help_text )174 required, widget, label, initial, help_text, func) 174 175 175 176 def clean(self, value): 176 177 if self.required and not value: