Ticket #3292: models_widgets.patch

File models_widgets.patch, 8.5 KB (added by Honza Král <Honza.Kral@…>, 18 years ago)
  • django/db/models/fields/__init__.py

     
    7070        core=False, rel=None, default=NOT_PROVIDED, editable=True,
    7171        prepopulate_from=None, unique_for_date=None, unique_for_month=None,
    7272        unique_for_year=None, validator_list=None, choices=None, radio_admin=None,
    73         help_text='', db_column=None):
     73        help_text='', db_column=None, widget=None):
    7474        self.name = name
    7575        self.verbose_name = verbose_name
    7676        self.primary_key = primary_key
     
    8686        self.radio_admin = radio_admin
    8787        self.help_text = help_text
    8888        self.db_column = db_column
     89        self.widget = widget
    8990
    9091        # Set db_index to True if the field has a relationship and doesn't explicitly set db_index.
    9192        self.db_index = db_index
     
    337338    def formfield(self, initial=None):
    338339        "Returns a django.newforms.Field instance for this database Field."
    339340        # TODO: This is just a temporary default during development.
    340         return forms.CharField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
     341        return forms.CharField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial, widget=self.widget)
    341342
    342343    def value_from_object(self, obj):
    343344        "Returns the value of this field in the given model instance."
     
    397398        return [oldforms.CheckboxField]
    398399
    399400    def formfield(self, initial=None):
    400         return forms.BooleanField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
     401        return forms.BooleanField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial, widget=self.widget)
    401402
    402403class CharField(Field):
    403404    def get_manipulator_field_objs(self):
     
    414415        return str(value)
    415416
    416417    def formfield(self, initial=None):
    417         return forms.CharField(max_length=self.maxlength, required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
     418        return forms.CharField(max_length=self.maxlength, required=not self.blank, label=capfirst(self.verbose_name), initial=initial, widget=self.widget)
    418419
    419420# TODO: Maybe move this into contrib, because it's specialized.
    420421class CommaSeparatedIntegerField(CharField):
     
    488489        return {self.attname: (val is not None and val.strftime("%Y-%m-%d") or '')}
    489490
    490491    def formfield(self, initial=None):
    491         return forms.DateField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
     492        return forms.DateField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial, widget=self.widget)
    492493
    493494class DateTimeField(DateField):
    494495    def to_python(self, value):
     
    549550                time_field: (val is not None and val.strftime("%H:%M:%S") or '')}
    550551
    551552    def formfield(self, initial=None):
    552         return forms.DateTimeField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
     553        return forms.DateTimeField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial, widget=self.widget)
    553554
    554555class EmailField(CharField):
    555556    def __init__(self, *args, **kwargs):
     
    566567        validators.isValidEmail(field_data, all_data)
    567568
    568569    def formfield(self, initial=None):
    569         return forms.EmailField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
     570        return forms.EmailField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial, widget=self.widget)
    570571
    571572class FileField(Field):
    572573    def __init__(self, verbose_name=None, name=None, upload_to='', **kwargs):
     
    701702        return [oldforms.IntegerField]
    702703
    703704    def formfield(self, initial=None):
    704         return forms.IntegerField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
     705        return forms.IntegerField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial, widget=self.widget)
    705706
    706707class IPAddressField(Field):
    707708    def __init__(self, *args, **kwargs):
     
    758759        return [oldforms.LargeTextField]
    759760
    760761    def formfield(self, initial=None):
    761         return forms.CharField(required=not self.blank, widget=forms.Textarea, label=capfirst(self.verbose_name), initial=initial)
     762        return forms.CharField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial, widget=self.widget or forms.Textarea)
    762763
    763764class TimeField(Field):
    764765    empty_strings_allowed = False
     
    801802        return {self.attname: (val is not None and val.strftime("%H:%M:%S") or '')}
    802803
    803804    def formfield(self, initial=None):
    804         return forms.TimeField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
     805        return forms.TimeField(required=not self.blank, label=capfirst(self.verbose_name), initial=initial, widget=self.widget)
    805806
    806807class URLField(CharField):
    807808    def __init__(self, verbose_name=None, name=None, verify_exists=True, **kwargs):
     
    818819        return "CharField"
    819820
    820821    def formfield(self, initial=None):
    821         return forms.URLField(required=not self.blank, verify_exists=self.verify_exists, label=capfirst(self.verbose_name), initial=initial)
     822        return forms.URLField(required=not self.blank, verify_exists=self.verify_exists, label=capfirst(self.verbose_name), initial=initial, widget=self.widget)
    822823
    823824class USStateField(Field):
    824825    def get_manipulator_field_objs(self):
  • django/db/models/fields/related.py

     
    549549        setattr(cls, related.get_accessor_name(), ForeignRelatedObjectsDescriptor(related))
    550550
    551551    def formfield(self, initial=None):
    552         return forms.ChoiceField(choices=self.get_choices_default(), required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
     552        return forms.ChoiceField(choices=self.get_choices_default(), required=not self.blank, label=capfirst(self.verbose_name), initial=initial, widget=self.widget)
    553553
    554554class OneToOneField(RelatedField, IntegerField):
    555555    def __init__(self, to, to_field=None, **kwargs):
     
    613613            cls._meta.one_to_one_field = self
    614614
    615615    def formfield(self, initial=None):
    616         return forms.ChoiceField(choices=self.get_choices_default(), required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
     616        return forms.ChoiceField(choices=self.get_choices_default(), required=not self.blank, label=capfirst(self.verbose_name), initial=initial, widget=self.widget)
    617617
    618618class ManyToManyField(RelatedField, Field):
    619619    def __init__(self, to, **kwargs):
     
    730730        # MultipleChoiceField takes a list of IDs.
    731731        if initial is not None:
    732732            initial = [i._get_pk_val() for i in initial]
    733         return forms.MultipleChoiceField(choices=self.get_choices_default(), required=not self.blank, label=capfirst(self.verbose_name), initial=initial)
     733        return forms.MultipleChoiceField(choices=self.get_choices_default(), required=not self.blank, label=capfirst(self.verbose_name), initial=initial, widget=self.widget)
    734734
    735735class ManyToOneRel(object):
    736736    def __init__(self, to, field_name, num_in_admin=3, min_num_in_admin=None,
  • django/newforms/fields.py

     
    316316        return bool(value)
    317317
    318318class ChoiceField(Field):
    319     def __init__(self, choices=(), required=True, widget=Select, label=None, initial=None):
     319    widget = Select
     320    def __init__(self, choices=(), required=True, widget=None, label=None, initial=None):
     321        widget = widget or self.widget
    320322        if isinstance(widget, type):
    321323            widget = widget(choices=choices)
    322324        Field.__init__(self, required, widget, label, initial)
     
    338340
    339341class MultipleChoiceField(ChoiceField):
    340342    hidden_widget = MultipleHiddenInput
     343    widget = SelectMultiple
    341344
    342     def __init__(self, choices=(), required=True, widget=SelectMultiple, label=None, initial=None):
     345    def __init__(self, choices=(), required=True, widget=None, label=None, initial=None):
    343346        ChoiceField.__init__(self, choices, required, widget, label, initial)
    344347
    345348    def clean(self, value):
Back to Top