Django admin, hide + Plus to certain foreignkey fields

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

Thanks in advance!

+6
source share
2 answers

+ added when this foreign key model can also be added by the administrator and is based on the permissions that the user has on this model. If the user cannot add these types of models, override has_add_permission with the foreign key ModelAdmin (i.e. the one that has a plus sign to allow you to add), and return False for the appropriate conditions. + will be unavailable to any user.

+3
source

If you just want to hide it for a cosmetic purpose, I would use a Javascript script that hides this โ€œ+โ€ sign.

You can add your own Javascript sources to the ModelManager of the application using the internal Media class, as described in docs . Something like that:

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

The Javascript source will look something like this:

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

On the other hand, if these admin users can never add such a model, do not give them permission to add them. Then these add links will never be displayed.

+2
source

Source: https://habr.com/ru/post/904747/


All Articles