diff --git a/README.rst b/README.rst index 6927112..6e473cb 100644 --- a/README.rst +++ b/README.rst @@ -276,6 +276,23 @@ Output: +Fields with multiple widgets +============================ + +Some fields may render as a `MultiWidget`, composed of multiple subwidgets +(for example, a `ChoiceField` using `RadioSelect`). You can use the same tags +and filters, but your template code will need to include a for loop for fields +like this: + +.. code-block:: html+django + + {% load widget_tweaks %} + + {% for widget in form.choice %} + {{ widget|add_class:"css_class_1 css_class_2" }} + {% endfor %} + + Mixing render_field and filters =============================== @@ -356,7 +373,3 @@ Make sure you have `tox `_ installed, then type from the source checkout. -NOT SUPPORTED -============= - -MultiWidgets: SplitDateTimeWidget, SplitHiddenDateTimeWidget diff --git a/widget_tweaks/templatetags/widget_tweaks.py b/widget_tweaks/templatetags/widget_tweaks.py index 116b3c3..cb1048e 100644 --- a/widget_tweaks/templatetags/widget_tweaks.py +++ b/widget_tweaks/templatetags/widget_tweaks.py @@ -24,7 +24,21 @@ def _process_field_attributes(field, attr, process): attribute = params[0].replace("::", ":") value = params[1] if len(params) == 2 else True field = copy(field) - # decorate field.as_widget method with updated attributes + # decorate field.as_widget or field.tag method with updated attributes + + if not hasattr(field, 'as_widget'): + old_tag = field.tag + + def tag(self, wrap_label=False): # pylint: disable=unused-argument + attrs = self.data['attrs'] + process(self.parent_widget, attrs, attribute, value) + html = old_tag(wrap_label=False) + self.tag = old_tag + return html + + field.tag = types.MethodType(tag, field) + return field + old_as_widget = field.as_widget def as_widget(self, widget=None, attrs=None, only_initial=False):