How can I remove the add and change buttons from a TabularInline admin field?
The OP's idea of setting the widget's attributes should work.
The basic idea is as follows:
The actual form field in the TabularInline
for AB
that allows you to select the B
object is a ModelChoiceField. This field has a Select widget, wrapped in a RelatedFieldWidgetWrapper. The latter controls the "add" and "change" (or "edit") buttons next to the select box. To remove these buttons, set the widget's can_add_related
and can_change_related
attributes to False
.
This is actually what the OP attempted to do. However, the OP tried to extend get_form
, but that method is only available on a ModelAdmin
, not on the TabularInline
, as far as I know (source).
Instead of using get_form
, we can extend e.g. formfield_for_dbfield
(source) on the TabularInline
, as illustrated below (based on OP's example):
class ABInline(admin.TabularInline):
model = AB
def formfield_for_dbfield(self, db_field, request, **kwargs):
formfield = super(ABInline, self).formfield_for_dbfield(
db_field, request, **kwargs)
if db_field.name == 'b':
# Assuming AB.b is the ForeignKey to B
formfield.widget.can_add_related = False
formfield.widget.can_change_related = False
# formfield.widget.can_delete_related = False # default is already False
return formfield
Here we assume that the OP's AB
model looks something like this:
class AB(models.Model):
a = models.ForeignKey(to='A', ...)
b = models.ForeignKey(to='B', ...)
...
You could try with this functions:
See docs
class ABInline(admin.TabularInline):
model = AB
def has_add_permission(self, request):
return False
def has_change_permission(self, request, obj=None):
return False
def has_delete_permission(self, request, obj=None):
return False