From b40ac3f637d002168f4d1bdefd30844e2823909a Mon Sep 17 00:00:00 2001 From: John Kyle Cronan Date: Thu, 6 Jan 2022 01:59:29 -0600 Subject: [PATCH 1/3] add support for fields with subwidgets (BoundWidget) --- widget_tweaks/templatetags/widget_tweaks.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/widget_tweaks/templatetags/widget_tweaks.py b/widget_tweaks/templatetags/widget_tweaks.py index 045e665..6629d2c 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): From 2cd14aed9c2cdc1286f06e8adf3f6fa4de6818ce Mon Sep 17 00:00:00 2001 From: John Kyle Cronan Date: Thu, 6 Jan 2022 02:01:40 -0600 Subject: [PATCH 2/3] update readme for new feature --- README.rst | 4 ---- 1 file changed, 4 deletions(-) diff --git a/README.rst b/README.rst index fd3d488..eb43bfd 100644 --- a/README.rst +++ b/README.rst @@ -318,7 +318,3 @@ Make sure you have `tox `_ installed, then type from the source checkout. -NOT SUPPORTED -============= - -MultiWidgets: SplitDateTimeWidget, SplitHiddenDateTimeWidget From 8ba973001563e0f6fe791834b84a61e555880f8d Mon Sep 17 00:00:00 2001 From: John Kyle Cronan Date: Fri, 25 Aug 2023 13:23:25 -0500 Subject: [PATCH 3/3] new README section for fields with multiple widgets --- README.rst | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.rst b/README.rst index 574d846..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 ===============================