Ticket #9386: 03-attrs-templates-legends.diff

File 03-attrs-templates-legends.diff, 5.7 KB (added by Petr Marhoun <petr.marhoun@…>, 6 years ago)
  • django/forms/fields.py

    === modified file 'django/forms/fields.py'
     
    5959    creation_counter = 0
    6060
    6161    def __init__(self, required=True, widget=None, label=None, initial=None,
    62                  help_text=None, error_messages=None, show_hidden_initial=False):
     62                 help_text=None, error_messages=None, show_hidden_initial=False,
     63                 attrs=None, template=None):
    6364        # required -- Boolean that specifies whether the field is required.
    6465        #             True by default.
    6566        # widget -- A Widget class, or instance of a Widget class, that should
     
    9394            widget.attrs.update(extra_attrs)
    9495
    9596        self.widget = widget
     97       
     98        self.attrs = attrs
     99        self.template = template
    96100
    97101        # Increase the creation counter, and save our local copy.
    98102        self.creation_counter = Field.creation_counter
  • django/forms/forms.py

    === modified file 'django/forms/forms.py'
     
    114114    # class, not to the Form class.
    115115    def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
    116116                 initial=None, error_class=ErrorList, label_suffix=':',
    117                  empty_permitted=False):
     117                 empty_permitted=False, attrs=None, template=None):
    118118        self.is_bound = data is not None or files is not None
    119119        self.data = data or {}
    120120        self.files = files or {}
     
    127127        self._is_valid = None # Stores validation state after full_clean() has been called.
    128128        self._changed_data = None
    129129
     130        self.attrs = attrs
     131        self.template = template
     132
    130133        # The base_fields class attribute is the *class-wide* definition of
    131134        # fields. Because a particular *instance* of the class might want to
    132135        # alter self.fields, we create self.fields here by copying base_fields.
     
    417420        else:
    418421            self.label = self.field.label
    419422        self.help_text = field.help_text or ''
     423        self.attrs = field.attrs
     424        self.template = field.template
    420425
    421426    def __unicode__(self):
    422427        """Renders this field as an HTML widget."""
  • django/forms/formsets.py

    === modified file 'django/forms/formsets.py'
     
    3030    A collection of instances of the same Form class.
    3131    """
    3232    def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
    33                  initial=None, error_class=ErrorList):
     33                 initial=None, error_class=ErrorList, attrs=None, template=None, legend=None):
    3434        self.is_bound = data is not None or files is not None
    3535        self.prefix = prefix or 'form'
    3636        self.auto_id = auto_id
     
    6262                       INITIAL_FORM_COUNT: self._initial_form_count}
    6363            self.management_form = ManagementForm(initial=initial, auto_id=self.auto_id, prefix=self.prefix)
    6464       
     65        self.attrs = attrs
     66        self.template = template
     67        self.legend = legend
     68       
    6569        # construct the forms in the formset
    6670        self._construct_forms()
    6771
  • django/forms/models.py

    === modified file 'django/forms/models.py'
     
    223223class BaseModelForm(BaseForm):
    224224    def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
    225225                 initial=None, error_class=ErrorList, label_suffix=':',
    226                  empty_permitted=False, instance=None):
     226                 empty_permitted=False, instance=None, attrs=None, template=None):
    227227        if instance is None:
    228228            # if we didn't get an instance, instantiate a new one
    229229            self.instance = self._meta.model()
     
    235235        if initial is not None:
    236236            object_data.update(initial)
    237237        super(BaseModelForm, self).__init__(data, files, auto_id, prefix, object_data,
    238                                             error_class, label_suffix, empty_permitted)
     238                                            error_class, label_suffix, empty_permitted,
     239                                            attrs, template)
    239240    def clean(self):
    240241        self.validate_unique()
    241242        return self.cleaned_data
     
    498499        self.save_as_new = save_as_new
    499500        # is there a better way to get the object descriptor?
    500501        self.rel_name = RelatedObject(self.fk.rel.to, self.model, self.fk).get_accessor_name()
    501         super(BaseInlineFormSet, self).__init__(data, files, prefix=prefix or self.rel_name)
     502        kwargs = {}
     503        for name in ('attrs', 'template', 'legend'):
     504            kwargs[name] = getattr(self.__class__, name, None)
     505        super(BaseInlineFormSet, self).__init__(data, files, prefix=prefix or self.rel_name, **kwargs)
    502506
    503507    def _construct_forms(self):
    504508        if self.save_as_new:
     
    571575                          formset=BaseInlineFormSet, fk_name=None,
    572576                          fields=None, exclude=None, fieldsets=None, inlines=None, formfield_kwargs=None,
    573577                          extra=3, can_order=False, can_delete=True, max_num=0,
    574                           formfield_callback=lambda f, **kwargs: f.formfield(**kwargs)):
     578                          formfield_callback=lambda f, **kwargs: f.formfield(**kwargs),
     579                          attrs=None, template=None, legend=None):
    575580    """
    576581    Returns an ``InlineFormSet`` for the given kwargs.
    577582
     
    603608    }
    604609    FormSet = modelformset_factory(model, **kwargs)
    605610    FormSet.fk = fk
     611    FormSet.attrs = attrs
     612    FormSet.template = template
     613    FormSet.legend = legend
    606614    return FormSet
    607615
    608616
Back to Top