Question

This question already has an answer here:

Is there a way to remove the "Add" functionality on the Django admin site? For certain entities, I only want the Django admin to be able to view them or change existing ones, but not add new ones.

Was it helpful?

Solution

Sure, you can customize admin VERY granularly by following the instructions here -- I believe that what you want can be obtained in part by overriding ModelAdmin.save_model(self, request, obj, form, change) in your own ModelAdmin subclass, to ensure nothing happens on the store when change is false (i.e. an attempt to add rather than change), and in part by overriding ModelAdmin.add_view(self, request, form_url='', extra_context=None) to display an "add view" that makes it very clear to the admin that they're NOT going to be allowed to add object through this route. I haven't actually done the specific admin customization you require, but I've done others and they do seem to work pretty smoothly!

OTHER TIPS

You can customize the permission for each user group from within the admin interface: try going to /admin/auth/group and it should be straightforward from there.

This won't be as granular as the solution offered by the earlier answer, but it will take care of most of your needs without needing to customize the admin.

If you change the permissions to restrict access then you'll still get the plus sign by a FK/MtM field. Clicking that will open a popup window with 'Permission Denied' in it.

You can actually completely remove the plus sign by not simply not registering the model with the admin.

I have a situation where I have predefined categories that I want users to be able to select more than one of. The best way to do this is with a models.ManyToMany field. You can register the model with the admin, enter the data as required and then remove the registration.

An easy effective way is to set max_num=0 for that particular inline.

Satya's suggestion of setting max_num=0 works perfectly.

Per the Django docs on the ModelForm class:

For users with JavaScript-enabled browsers, an "Add another" link is provided to enable any number of additional inlines to be added in addition to those provided as a result of the extra argument.

The dynamic link will not appear if the number of currently displayed forms exceeds max_num, or if the user does not have JavaScript enabled.

and

As with regular formsets, you can use the max_num and extra parameters to modelformset_factory to limit the number of extra forms displayed.

max_num does not prevent existing objects from being displayed

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top