Restricting Model Add Actions in Django Admin

The Django admin interface provides a convenient way to manage database content by automatically generating interfaces to create, view, edit, and delete records. By default, the admin allows all database operations on all models. However, in some cases, you may want to restrict the ability to add new records for certain models.

Why Restrict Add Actions

There are a few reasons why you may want to disable the ability to add objects of a particular model through the admin interface:

Firstly, some models represent reference data or configuration that should not change frequently. For example, if you have a model representing product categories, you may not want regular admin users randomly adding new categories.

Secondly, data consistency and integrity reasons. If you have defined certain validation rules or dependencies between models, uncontrolled adds could violate those. Disabling the add action forces changes to go through established business processes.

Additionally, access control and security concerns may dictate locking down writes to sensitive data. For auditing or compliance purposes, adds may need review, approval etc. Removing the admin add action assists in enforcing such policies.

How to Disable Model Add in Django Admin

Using ModelAdmin Options

The easiest way to disable add actions is by using Django ModelAdmin options. Specifically, we can set the has_add_permission attribute to False:

from django.contrib import admin

@admin.register(MyModel) 
class MyModelAdmin(admin.ModelAdmin):
    has_add_permission = False

This will remove the “+” drop down action next to the model in the admin site header. As well as hide the add button on the model’s changelist page.

Override Permissions

A more advanced approach is to override the admin views permissions. By default, admins have wide permissions to add, change and delete all models.

We can override the permissions at either the model or admin class level:

class MyModelAdmin(admin.ModelAdmin):
    def has_add_permission(self, request):
        return False

@admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):  
    def has_add_permission(self, request, obj=None):
        return False

This gives full control to implement any custom logic around add permissions. For example, checking if a user has a certain role or permission before allowing adds.

Limiting Available Admin Classes

Finally, add actions can be restricted by controlling which admin interfaces are registered with models.

For example, Django ships with a ReadOnlyAdmin class that only permits viewing and editing of existing objects. We can use this to lock down a model:

from django.contrib.auth.admin import ReadOnlyAdmin

@admin.register(MyModel)
class MyModel(ReadOnlyAdmin):
    pass

This approach works well for reference data that needs to be managed through special processes outside the admin UI.

When To Avoid Restricting Adds

There are times when disabling admin add actions may be inadvisable:

  • For models rarely accessed outside the admin interface and where data consistency is not critical. Otherwise it creates extra work to manually add records.
  • Applications with simple permissions where all admins have equal access rights. Additional access control may be unnecessary complexity.
  • When other create/update workflows don’t exist. Removing admin add can completely prevent creating essential seed or test data.

Conclusion

The Django admin interface provides convenient CRUD functionality by default. However customizable permissions give full control. Setting has_add_permission = False selectively removes the ability for users to freely add records.

Overriding create permissions or using ReadOnlyAdmin allows locking models down completely. This prevents unwanted changes to sensitive or reference data. Nonetheless, it’s still important to consider limitations before wholesale removal of write permissions.

As with many aspects of Django, flexible auth and permissions systems enable granular administration access controls. Developers can strike the right balance between usability and data security for each use case. Carefully restricting add ability using these various methods supports building robust and hardened web applications.