Ticket #7475: models.2.py.diff

File models.2.py.diff, 1.4 KB (added by Jason Davies, 16 years ago)

Add support for cache_choices option.

  • django/newforms/models.py

     
    285285    def __iter__(self):
    286286        if self.field.empty_label is not None:
    287287            yield (u"", self.field.empty_label)
    288         for obj in self.queryset:
    289             yield (obj.pk, self.field.label_from_instance(obj))
    290         # Clear the QuerySet cache if required.
    291         if not self.field.cache_choices:
    292             self.queryset._result_cache = None
     288        if self.field.cache_choices:
     289            if not self.field.choice_cache:
     290                self.field.choice_cache = [
     291                    (obj.pk, self.field.label_from_instance(obj))
     292                    for obj in self.queryset.all()
     293                ]
     294            for choice in self.field.choice_cache:
     295                yield choice
     296        else:
     297            for obj in self.queryset.all():
     298                yield (obj.pk, self.field.label_from_instance(obj))
    293299
    294300class ModelChoiceField(ChoiceField):
    295301    """A ChoiceField whose choices are a model QuerySet."""
     
    311317        Field.__init__(self, required, widget, label, initial, help_text,
    312318                       *args, **kwargs)
    313319        self.queryset = queryset
     320        self.choice_cache = None
    314321
    315322    def _get_queryset(self):
    316323        return self._queryset
Back to Top