Opened 8 months ago
Last modified 8 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 , 8 months ago
| Summary: | `admin.widgets.RelatedFieldWidgetWrapper.get_context()` bypasses the used renderer → admin.widgets.RelatedFieldWidgetWrapper.get_context() bypasses the used renderer |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
| Version: | 5.2 → dev |
comment:2 by , 8 months ago
| Owner: | set to |
|---|---|
| Status: | new → assigned |
comment:3 by , 8 months ago
| Has patch: | set |
|---|
comment:4 by , 8 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!
comment:5 by , 8 months ago
| Patch needs improvement: | set |
|---|
comment:6 by , 8 months ago
| Patch needs improvement: | unset |
|---|
comment:7 by , 8 months ago
| Needs tests: | set |
|---|---|
| Patch needs improvement: | set |
Thank you for the report! This makes sense to me but might be a little fiddly to resolve