Ticket #4620: queryset-lambda.diff

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

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

  • django/newforms/models.py

    old new  
    107107    return type('FormForFields', (BaseForm,), {'base_fields': fields})
    108108
    109109class QuerySetIterator(object):
    110     def __init__(self, queryset, empty_label, cache_choices):
    111         self.queryset, self.empty_label, self.cache_choices = queryset, empty_label, cache_choices
     110    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
    112112
    113113    def __iter__(self):
    114114        if self.empty_label is not None:
    115115            yield (u"", self.empty_label)
    116116        for obj in self.queryset:
    117             yield (obj._get_pk_val(), str(obj))
     117            yield (obj._get_pk_val(), self.func(obj))
    118118        # Clear the QuerySet cache if required.
    119119        if not self.cache_choices:
    120120            self.queryset._result_cache = None
     
    124124    # This class is a subclass of ChoiceField for purity, but it doesn't
    125125    # actually use any of ChoiceField's implementation.
    126126    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)):
    128128        self.queryset = queryset
    129129        self.empty_label = empty_label
    130130        self.cache_choices = cache_choices
     131        self.func = func
    131132        # Call Field instead of ChoiceField __init__() because we don't need
    132133        # ChoiceField.__init__().
    133134        Field.__init__(self, required, widget, label, initial, help_text)
     
    144145        # *each* time _get_choices() is called (and, thus, each time
    145146        # self.choices is accessed) so that we can ensure the QuerySet has not
    146147        # 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)
    148149
    149150    def _set_choices(self, value):
    150151        # This method is copied from ChoiceField._set_choices(). It's necessary
     
    168169    "A MultipleChoiceField whose choices are a model QuerySet."
    169170    hidden_widget = MultipleHiddenInput
    170171    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)):
    172173        super(ModelMultipleChoiceField, self).__init__(queryset, None, cache_choices,
    173             required, widget, label, initial, help_text)
     174            required, widget, label, initial, help_text, func)
    174175
    175176    def clean(self, value):
    176177        if self.required and not value:
Back to Top