Django RelatedFieldWidgetWrapper

The Django admin interface represents an Foreign Key field, as a select with a + button. This allows you to select an existing item from the foreign table, or create a new one with a pop up window. Very useful.

I wanted to add an edit button, so that you could select one of the items and click a button to edit that item in a pop up. I initially looked at the widget that displays the <select> which is django.forms.widgets.Select. However this literally only renders the <select>, not the + button.  So I then grepped the whole of the django source to find the code that renders the button. It is django.contrib.admin.widgets.RelatedFieldWidgetWrapper. So I wrote my own subclass of RelatedFieldWidgetWrapper and looked for the place to specify it. ModelAdmin.formfield_overrides was the most most obvious place. The catch is that, that requires a widget, my custom sublcass is not a widget, its a widget wrapper. RelatedFieldWidgetWrapper is only used once, and is hard coded in django.contrib.admin.options, so I can’t use my sub class without making changes to core 🙁

Next option is to add the link via javascript in an admin template override.