Add custom actions to soft delete records in django admin

Django admin provides some default actions to perform in bulk in list view. You can add a custom action in django admin by adding definition in admin.py file. You can add a short description which will be shown in the dropdown menu. You can see custom action for active/inactive records in bulk in the image below.

add-custom-actions

You can add a check of allowed permissions. This will show the action to the user if the user has the permissions defined in allowed_permissions. For example, make_active action will be visible to user if user has the “change” permission.

Both short_description and allowed_permissions are optional.

def make_active(modeladmin, request, queryset):
   queryset.update(is_active=True)

make_active.short_description = "Mark selected records as active"
make_active.allowed_permissions = ('change',) # users who have permission to make changes can view this action

Now, to use this custom action, add this(make_active) to modelAdmin.actions list.

@admin.register(Users)
class UsersAdmin(admin.ModelAdmin):
   actions = [make_active]

   class Meta:
       model = Users

add custom action to soft delete records

Here is another custom action to soft delete records from the database. Add below definition in your admin.py file.

def softdelete_selected(modeladmin, request, queryset):
   queryset.update(is_deleted=True)

softdelete_selected.short_description = "Delete Selected records"
softdelete_selected.allowed_permissions = ('change',) # users who have permission to make changes can view this action

@admin.register(Users)
class UsersAdmin(admin.ModelAdmin):
   actions = [make_active, softdelete_selected]

   class Meta:
       model = Users

Merge all the codes shown above to add following functionalities:

# disable delete_selected globally

# custom action to mark all records active

# custom action to soft delete records

# set has_delete_permission to False to remove delete button from the object detail page

# add softdelete_selected in modelAdmin.actions list to show custom actions in list view dropdown menu

# do not show deleted records in the list view

# add a column is_deleted in the DB schema

#globally disable delete_selected action
admin.site.disable_action('delete_selected')

def make_active(modeladmin, request, queryset):
   queryset.update(is_active=True)

make_active.short_description = "Mark selected records as active"
make_active.allowed_permissions = ('change',) # users who have change permission can view this action

def softdelete_selected(modeladmin, request, queryset):
   queryset.update(is_active=False)

softdelete_selected.short_description = "Delete Selected records"
softdelete_selected.allowed_permissions = ('change',) # users who have change permission can view this action


@admin.register(Users)
class UsersAdmin(admin.ModelAdmin):

   #set delete permission to false to remove delete button from the detail page
   def has_delete_permission(self, request, obj=None):
       return False

   #Show these actions in list view dropdown menu
   actions = [make_active, softdelete_selected]

   #get records in the list view where is_deleted=False
   def get_queryset(self, request):
       qs = super(Users, self).get_queryset(request)
       return qs.filter(is_deleted=False)

   class Meta:
       model = Users

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to Top
Shares