I added an action in my admin pages that allows user to export selected records to an excel sheet. Now I need to be able to only allow some users to be able to export data. I have a UserProfile
model that has a can_export
boolean field.
How can I only show the "Export To Excel" action in django admin only if they have the can_export
field set as True?
I have tried to find a way in admin.py to get the request object and do a IF statement before setting actions but have had no luck. I get a name'request' is not defined error of course.
if request.user.get_profile().can_export:
actions = [export_data()]
From the FineManual (https://docs.djangoproject.com/en/1.5/ref/contrib/admin/actions/):
class MyModelAdmin(admin.ModelAdmin):
...
def get_actions(self, request):
actions = super(MyModelAdmin, self).get_actions(request)
if request.user.username[0].upper() != 'J':
if 'delete_selected' in actions:
del actions['delete_selected']
return actions