From 89a2216486fa8a0513cbb1d49d2d587d4116c60b Mon Sep 17 00:00:00 2001 From: redodo Date: Mon, 19 Nov 2018 19:11:04 +0100 Subject: [PATCH] Fixed #29961 -- Made RelatedFieldWidgetWrapper hide related item links if wrapping a hidden widget. --- AUTHORS | 1 + .../admin/widgets/related_widget_wrapper.html | 2 ++ django/contrib/admin/widgets.py | 1 + tests/admin_widgets/tests.py | 23 +++++++++++++++++++ 4 files changed, 27 insertions(+) diff --git a/AUTHORS b/AUTHORS index 0c6994b5ea..a52ba34954 100644 --- a/AUTHORS +++ b/AUTHORS @@ -329,6 +329,7 @@ answer newbie questions, and generally made Django that much better: Helen Sherwood-Taylor Henrique Romano Henry Dang + Hidde Bultsma Himanshu Chauhan hipertracker@gmail.com Hiroki Kiyohara diff --git a/django/contrib/admin/templates/admin/widgets/related_widget_wrapper.html b/django/contrib/admin/templates/admin/widgets/related_widget_wrapper.html index 281d331443..7cdcb334c9 100644 --- a/django/contrib/admin/templates/admin/widgets/related_widget_wrapper.html +++ b/django/contrib/admin/templates/admin/widgets/related_widget_wrapper.html @@ -3,6 +3,7 @@ {{ rendered_widget }} {% block links %} {% spaceless %} + {% if not is_hidden %} {% if can_change_related %} {% endif %} + {% endif %} {% endspaceless %} {% endblock %} diff --git a/django/contrib/admin/widgets.py b/django/contrib/admin/widgets.py index 9385104d59..81dbcaf236 100644 --- a/django/contrib/admin/widgets.py +++ b/django/contrib/admin/widgets.py @@ -285,6 +285,7 @@ class RelatedFieldWidgetWrapper(forms.Widget): ]) context = { 'rendered_widget': self.widget.render(name, value, attrs), + 'is_hidden': self.is_hidden, 'name': name, 'url_params': url_params, 'model': rel_opts.verbose_name, diff --git a/tests/admin_widgets/tests.py b/tests/admin_widgets/tests.py index f7c2a7865c..623fa39bc1 100644 --- a/tests/admin_widgets/tests.py +++ b/tests/admin_widgets/tests.py @@ -687,6 +687,29 @@ class RelatedFieldWidgetWrapperTests(SimpleTestCase): wrapper = widgets.RelatedFieldWidgetWrapper(widget, rel, widget_admin_site) self.assertIs(wrapper.value_omitted_from_data({}, {}, 'band'), False) + def test_widget_is_hidden(self): + rel = Album._meta.get_field('band').remote_field + widget = forms.HiddenInput() + widget.choices = () + wrapper = widgets.RelatedFieldWidgetWrapper(widget, rel, widget_admin_site) + self.assertIs(wrapper.is_hidden, True) + context = wrapper.get_context('band', None, {}) + self.assertIs(context['is_hidden'], True) + output = wrapper.render('name', 'value') + # Related item links are hidden. + self.assertNotIn('