Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove "add another" in Django admin screen

Whenever I'm editing object A with a foreign key to object B, a plus option "add another" is available next to the choices of object B. How do I remove that option?

I configured a user without rights to add object B. The plus sign is still available, but when I click on it, it says "Permission denied". It's ugly.

I'm using Django 1.0.2

like image 224
Jack Ha Avatar asked Nov 12 '09 09:11

Jack Ha


People also ask

How do you remove save and add another Django admin?

The simplest option is to set save_as=True on the ModelAdmin . This will replace the "Save and add another" button with a "Save as new" button.

How can I remove extra's from Django admin panel?

Take a look at the Model Meta in the django documentation. Within a Model you can add class Meta this allows additional options for your model which handles things like singular and plural naming. Show activity on this post. inside model.py or inside your customized model file add class meta within a Model Class.

How do I restrict access to admin pages in Django?

Django admin allows access to users marked as is_staff=True . To disable a user from being able to access the admin, you should set is_staff=False . This holds true even if the user is a superuser. is_superuser=True .


2 Answers

The following answer was my original answer but it is wrong and does not answer OP's question:

Simpler solution, no CSS hack and no editing Django codebase:

Add this to your Inline class:

max_num=0 

(this is only applicable to inline forms, not foreign key fields as OP asked)


The above answer is only useful to hide the "add related" button for inline forms, and not foreign keys as requested.

When I wrote the answer, IIRC the accepted answer hid both, which is why I got confused.

The following seems to provide a solution (though hiding using CSS seems the most feasible thing to do, especially if the "add another" buttons of FKs are in inline forms):

Django 1.7 removing Add button from inline form

like image 149
pistache Avatar answered Sep 30 '22 13:09

pistache


Though most of the solutions mentioned here work, there is another cleaner way of doing it. Probably it was introduced in a later version of Django, after the other solutions were presented. (I'm presently using Django 1.7)

To remove the "Add another" option,

class ... #(Your inline class)      def has_add_permission(self, request):         return False 

Similarly if you want to disable "Delete?" option, add the following method in Inline class.

    def has_delete_permission(self, request, obj=None):         return False 
like image 25
Sudipta Avatar answered Sep 30 '22 14:09

Sudipta