Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for fields with subwidgets (BoundWidget) #122

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,23 @@ Output:
</div>


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
===============================

Expand Down Expand Up @@ -356,7 +373,3 @@ Make sure you have `tox <http://tox.testrun.org/>`_ installed, then type

from the source checkout.

NOT SUPPORTED
=============

MultiWidgets: SplitDateTimeWidget, SplitHiddenDateTimeWidget
16 changes: 15 additions & 1 deletion widget_tweaks/templatetags/widget_tweaks.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,21 @@
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

Check warning on line 30 in widget_tweaks/templatetags/widget_tweaks.py

View check run for this annotation

Codecov / codecov/patch

widget_tweaks/templatetags/widget_tweaks.py#L30

Added line #L30 was not covered by tests

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

Check warning on line 37 in widget_tweaks/templatetags/widget_tweaks.py

View check run for this annotation

Codecov / codecov/patch

widget_tweaks/templatetags/widget_tweaks.py#L32-L37

Added lines #L32 - L37 were not covered by tests

field.tag = types.MethodType(tag, field)
return field

Check warning on line 40 in widget_tweaks/templatetags/widget_tweaks.py

View check run for this annotation

Codecov / codecov/patch

widget_tweaks/templatetags/widget_tweaks.py#L39-L40

Added lines #L39 - L40 were not covered by tests

old_as_widget = field.as_widget

def as_widget(self, widget=None, attrs=None, only_initial=False):
Expand Down
Loading