Opened 6 months ago

Last modified 6 months ago

#36250 assigned Bug

admin.widgets.RelatedFieldWidgetWrapper.get_context() bypasses the used renderer

Reported by: Jacob Rief Owned by: Ahmed Nassar
Component: contrib.admin Version: dev
Severity: Normal Keywords:
Cc: Jacob Rief Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

In django.contrib.admin, ForeignKey and ManyToManyField are mapped to a admin.widgets.RelatedFieldWidgetWrapper. This wrapper widget then renders the underling widget using:

def get_context(self, name, value, attrs):
    ...
    context = {
       "rendered_widget": self.widget.render(name, value, attrs),
        ...
    }
    ...

If the RelatedFieldWidgetWrapper itself has been rendered using an alternative renderer, the underling widgets will be rendered using the default renderer instead of the alternative renderer as used to render the said wrapper widget. This may lead to unexpected output.

Possible solution:

By adding the optional argument renderer to the get_context()-method, the widget rendering function could be rewritten as self.widget.render(name, value, attrs, renderer=renderer).

This issue might be related to #34257.

Change History (7)

comment:1 by Sarah Boyce, 6 months ago

Summary: `admin.widgets.RelatedFieldWidgetWrapper.get_context()` bypasses the used rendereradmin.widgets.RelatedFieldWidgetWrapper.get_context() bypasses the used renderer
Triage Stage: UnreviewedAccepted
Version: 5.2dev

Thank you for the report! This makes sense to me but might be a little fiddly to resolve

comment:2 by Ahmed Nassar, 6 months ago

Owner: set to Ahmed Nassar
Status: newassigned

comment:3 by Ahmed Nassar, 6 months ago

Has patch: set

comment:4 by Ahmed Nassar, 6 months ago

✅ Update on RelatedFieldWidgetWrapper Renderer Fix

I have submitted a PR to ensure RelatedFieldWidgetWrapper.get_context() correctly respects the provided renderer.

🔹 Fixes:

Added an optional renderer parameter.
Ensured the underlying widget uses the correct renderer.
Maintained full backward compatibility.
All tests passed successfully.

📌 The PR is ready for review. Let me know if any changes are needed!

🔗 PR: https://github.com/django/django/pull/19271

Version 0, edited 6 months ago by Ahmed Nassar (next)

comment:5 by Sarah Boyce, 6 months ago

Patch needs improvement: set

comment:6 by Ahmed Nassar, 6 months ago

Patch needs improvement: unset

comment:7 by Sarah Boyce, 6 months ago

Needs tests: set
Patch needs improvement: set
Note: See TracTickets for help on using tickets.
Back to Top