Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hide "add", "change", "delete" actions in djaongo admin

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?

like image 767
MarcS82 Avatar asked Feb 06 '26 10:02

MarcS82


1 Answers

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
like image 77
Mikey Lockwood Avatar answered Feb 09 '26 08:02

Mikey Lockwood



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!