Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django admin, hide +Plus sign to specific foreignkey field

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!

like image 810
Torgia Avatar asked Dec 30 '11 12:12

Torgia


2 Answers

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.

like image 129
Chris Pratt Avatar answered Nov 20 '22 02:11

Chris Pratt


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.

like image 43
Haes Avatar answered Nov 20 '22 02:11

Haes