Veehmot Veehmot - 5 days ago 7
Python Question

@staff_member_required is throwing "object has no attribute 'user'"

I'm using this decorator to make this page staff-only:

class MyModelAdmin(admin.ModelAdmin):

@staff_member_required
def dostuff(self, request):
return HttpResponse("This is secret")

def get_urls(self):
urls = super(MyModelAdmin, self).get_urls()
my_urls = [
url(r"^dostuff/$", self.dostuff)
]
return my_urls + urls


But for some reason I'm getting:

AttributeError: 'MyModelAdmin' object has no attribute 'user'


Without the decorator, the view works fine (but anyone can see it).

Answer

You need to use method_decorator():

from django.utils.decorators import method_decorator

class MyModelAdmin(admin.ModelAdmin):

    @method_decorator(staff_member_required)
    def dostuff(self, request):
        return HttpResponse("This is secret")
Comments