How to suppress icon_addlink.gif (RelatedFieldWidgetWrapper) in admin interfaces
|Reported by:||gcc||Owned by:||nobody|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||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 db_field.name not in self.raw_id_fields: related_modeladmin = self.admin_site._registry.get( db_field.rel.to) can_add_related = bool(related_modeladmin and related_modeladmin.has_add_permission(request)) formfield.widget = widgets.RelatedFieldWidgetWrapper( formfield.widget, db_field.rel, self.admin_site, can_add_related=can_add_related) 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, **kwargs) 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.