I have a piecce of code like this:
class PlatformEnvInLine(admin.TabularInline):
model = PlatformEnv
extra = 1
classes = ['collapse']
fields = ('environment',)
My PlatformEnv Model looks like this:
class PlatformEnv(models.Model):
id = models.AutoField(db_column='ID', primary_key=True)
...
environment = models.ForeignKey(Environment, models.DO_NOTHING, db_column='Environment_ID', blank=True, null=True)
When the PlatformEnvInLin is shown now, you can select an environment via dropdown. But next to the dropdown there are buttons displayed to add, change or delete a environment. How can I hide these buttons?
You should be able to do this by overriding the formfield_for_dbfield method.
class PlatformEnvInLine(admin.TabularInline):
model = PlatformEnv
extra = 1
classes = ['collapse']
fields = ('environment',)
def formfield_for_dbfield(self, db_field, request, **kwargs):
formfield = super().formfield_for_dbfield(db_field, request, **kwargs)
if db_field.name == 'environment':
formfield.widget.can_add_related = False
formfield.widget.can_change_related = False
formfield.widget.can_delete_related = False
return formfield
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With