5.5. Admin Actions
5.5.1. Example
>>>
... from django.contrib import admin
... from django.utils.translation import gettext_lazy as _
...
...
... @admin.register(Customer)
... class CustomerAdmin(admin.ModelAdmin):
... actions = ['make_active', 'make_inactive']
...
... @admin.action(description=_('Make active'))
... def make_active(self, request, queryset):
... queryset.update(is_active=True)
...
... @admin.action(description=_('Make inactive'))
... def make_inactive(self, request, queryset):
... queryset.update(is_active=False)
5.5.2. Permissions
>>>
... from django.contrib import admin
... from django.utils.translation import gettext_lazy as _
... from django.contrib.auth import get_permission_codename
...
...
... class CustomerAdmin(admin.ModelAdmin):
... actions = ['make_inactive']
...
... @admin.action(permissions=['change'], description=_('Make inactive'))
... def make_inactive(self, request, queryset):
... queryset.update(is_active=False)
5.5.3. Use Case - 1
>>>
... from django.contrib import admin
... from django.utils.translation import gettext_lazy as _
...
...
... class CustomerAdmin(admin.ModelAdmin):
... actions = ['export_as_csv']
...
... @admin.action(permissions=['view'], description=_('Export selected as CSV'))
... def export_as_csv(self, modeladmin, request, queryset):
... response = HttpResponse(content_type='text/csv')
... model = self.model._meta
... field_names = [field.name for field in model.fields]
... writer = csv.writer(response, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL, lineterminator='\n')
... writer.writerow(field_names)
... for obj in queryset.order_by('id'):
... writer.writerow([getattr(obj, field) for field in field_names])
... filename = f'{date.today():%Y-%m-%d}_export_{model}.csv'
... response['Content-Disposition'] = f'attachment; filename={filename}'
... return response
5.5.4. Use Case - 2
>>>
... from django.contrib import admin
... from django.utils.translation import gettext_lazy as _
...
...
... class ExportCsvMixin:
... @admin.action(permissions=['view'], description=_('Export selected as CSV'))
... def export_as_csv(self, modeladmin, request, queryset):
... response = HttpResponse(content_type='text/csv')
... model = self.model._meta
... field_names = [field.name for field in model.fields]
... writer = csv.writer(response, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL, lineterminator='\n')
... writer.writerow(field_names)
... for obj in queryset.order_by('id'):
... writer.writerow([getattr(obj, field) for field in field_names])
... filename = f'{date.today():%Y-%m-%d}_export_{model}.csv'
... response['Content-Disposition'] = f'attachment; filename={filename}'
... return response
...
... def get_actions(self, request):
... actions = super().get_actions(request)
... actions['export_as_csv'] = (self.export_as_csv, 'export_as_csv', self.export_as_csv.short_description)
... return actions
...
...
... class CustomerAdmin(ExportCsvMixin, admin.ModelAdmin):
... pass
5.5.5. Use Case - 3
>>>
... from django.contrib import admin
... from django.utils.translation import gettext_lazy as _
... from shop.models import Customer
...
...
... @admin.register(Customer)
... class CustomerAdmin(UserAdmin, AssignRoleActionsMixin):
... actions = ['assign_role_superuser', 'assign_role_staff', 'assign_role_user']
...
... @admin.action(permissions=['change'], description=_('Assign role Superuser'))
... def assign_role_superuser(self, request, queryset):
... return queryset.update(is_superuser=True, is_staff=True, is_active=True)
...
... @admin.action(permissions=['change'], description=_('Assign role Staff'))
... def assign_role_staff(self, request, queryset):
... return queryset.update(is_superuser=False, is_staff=True, is_active=True)
...
... @admin.action(permissions=['change'], description=_('Assign role User'))
... def assign_role_user(self, request, queryset):
... return queryset.update(is_superuser=False, is_staff=False, is_active=True)