Opened 9 years ago

Closed 9 years ago

#17547 closed New feature (duplicate)

How to suppress icon_addlink.gif (RelatedFieldWidgetWrapper) in admin interfaces

Reported by: Chris Wilson Owned by: nobody
Component: contrib.admin Version: 1.3
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


BaseModelAdmin's formfield_for_dbfield() method always wraps relations to other tables (ForeignKey and ManyToManyField) in a RelatedFieldWidgetWrapper, which renders a green Add button next to the field.

You might not want to show this to users, if you're building your interface on top of the admin interface to get automatic CRUD operations. However it's difficult to prevent:

    def formfield_for_dbfield(self, db_field, **kwargs):
        if isinstance(db_field, (models.ForeignKey, models.ManyToManyField)):
            # For non-raw_id fields, wrap the widget with a wrapper that adds
            # extra HTML -- the "add other" interface -- to the end of the
            # rendered output. formfield can be None if it came from a
            # OneToOneField with parent_link=True or a M2M intermediary.
            if formfield and not in self.raw_id_fields:
                related_modeladmin = self.admin_site._registry.get(
                can_add_related = bool(related_modeladmin and
                formfield.widget = widgets.RelatedFieldWidgetWrapper(
                            formfield.widget, db_field.rel, self.admin_site,

            return formfield

I worked around this by overriding formfield_for_dbfield to undo the wrapping:

class DocumentAdmin(admin.ModelAdmin):
    def formfield_for_dbfield(self, db_field, **kwargs):
        old_formfield = admin.ModelAdmin.formfield_for_dbfield(self, db_field,
        if (hasattr(old_formfield, 'widget') and
            isinstance(old_formfield.widget, RelatedFieldWidgetWrapper)):
            old_formfield.widget.can_add_related = False
        return old_formfield

However it would be nice to have a keyword argument to disable this behaviour.

Attachments (1)

django-baseadmin-addlink-optional.patch (1.2 KB) - added by Chris Wilson 9 years ago.
patch to add this feature

Download all attachments as: .zip

Change History (2)

Changed 9 years ago by Chris Wilson

patch to add this feature

comment:1 Changed 9 years ago by Julien Phalip

Resolution: duplicate
Status: newclosed

Thanks for the report. In fact this was already reported in #9071.

Note: See TracTickets for help on using tickets.
Back to Top