Ticket #7244: 0001-Fixed-widget-handling-in-ModelChoicesField.__init__.patch

File 0001-Fixed-widget-handling-in-ModelChoicesField.__init__.patch, 2.5 KB (added by Sebastian Noack, 17 years ago)
  • django/newforms/models.py

    From c7291fd0afe7c6b42f205a44f84b97151abf9a52 Mon Sep 17 00:00:00 2001
    From: Sebastian Noack <sebastian.noack@gmail.com>
    Date: Thu, 15 May 2008 16:18:23 +0200
    Subject: [PATCH] Fixed widget handling in ModelChoicesField.__init__ and ModelMultipleChoicesField.__init__ (#7244).
    
    ---
     django/newforms/models.py |   16 +++++++++++-----
     1 files changed, 11 insertions(+), 5 deletions(-)
    
    diff --git a/django/newforms/models.py b/django/newforms/models.py
    index 78a2041..d6451ee 100644
    a b from django.core.exceptions import ImproperlyConfigured  
    1313from util import ValidationError, ErrorList
    1414from forms import BaseForm, get_declared_fields
    1515from fields import Field, ChoiceField, EMPTY_VALUES
    16 from widgets import Select, SelectMultiple, MultipleHiddenInput
     16from widgets import MultipleHiddenInput
    1717
    1818__all__ = (
    1919    'ModelForm', 'BaseModelForm', 'model_to_dict', 'fields_for_model',
    class ModelChoiceField(ChoiceField):  
    301301    }
    302302
    303303    def __init__(self, queryset, empty_label=u"---------", cache_choices=False,
    304                  required=True, widget=Select, label=None, initial=None,
     304                 required=True, widget=None, label=None, initial=None,
    305305                 help_text=None, *args, **kwargs):
    306306        self.empty_label = empty_label
    307307        self.cache_choices = cache_choices
    class ModelChoiceField(ChoiceField):  
    346346        # the queryset.
    347347        return ModelChoiceIterator(self)
    348348
    349     choices = property(_get_choices, ChoiceField._set_choices)
     349    def _set_choices(self, value):
     350        # This method is copied from ChoiceField._set_choices(). It's necessary
     351        # because property() doesn't allow a subclass to overwrite only
     352        # _get_choices without implementing _set_choices.
     353        self._choices = self.widget.choices = list(value)
     354
     355    choices = property(_get_choices, _set_choices)
    350356
    351357    def clean(self, value):
    352358        Field.clean(self, value)
    class ModelMultipleChoiceField(ModelChoiceField):  
    368374    }
    369375
    370376    def __init__(self, queryset, cache_choices=False, required=True,
    371                  widget=SelectMultiple, label=None, initial=None,
    372                  help_text=None, *args, **kwargs):
     377                 widget=None, label=None, initial=None, help_text=None,
     378                 *args, **kwargs):
    373379        super(ModelMultipleChoiceField, self).__init__(queryset, None,
    374380            cache_choices, required, widget, label, initial, help_text,
    375381            *args, **kwargs)
Back to Top