Django admin, hide +Plus sign to specific foreignkey field
Asked Answered
S

3

7

i want to hide the plus + sign in some foreignkey fields of a specific model in django-admin interface. it's possible?

Thanks in advance!

Stern answered 30/12, 2011 at 12:55 Comment(1)
This question has a duplicate with a very good answer hereBridal
L
5

The + is added when that foreign key's model can also be added in the admin, and is based on the permissions the user has on that model. If the user shouldn't be able to add those types of models, override has_add_permission on the foreign key's ModelAdmin (i.e. the one the plus sign would allow you to add), and return False for the appropriate conditions. The + will go away for any user not allowed.

Luteous answered 30/12, 2011 at 15:29 Comment(0)
K
2

If you just want to hide it for cosmetic purpose, I'd use a Javascript script that hides this '+' sign.

You can add custom Javascript sources to Admin Modelform's by using the Media inner class, as described in the docs. Something like this:

class MyModelAdmin(admin.ModelAdmin):
    class Media:
        js = ("js/hide_myfield_addlink.js",)

The Javascript source would look something like:

/* file: hide_myfield_addlink.js */
django.jQuery(document).ready(function() {
    django.jQuery("#add_id_myfield").hide();
});

On the other hand, if those admin users should never be able to add such a model, don't give them the permission to add those. Then these add links will never be displayed.

Kampmann answered 30/12, 2011 at 14:12 Comment(1)
Might as well do it in CSS: .field-myfield .related-widget-wrapper-link {display: none;}Letterpress
L
0

Might as well do it in CSS:

.field-myfield .related-widget-wrapper-link {
  display: none;
}

Or to disable it everywhere:

.related-widget-wrapper-link {
  display: none;
}
Letterpress answered 26/1, 2021 at 20:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.