AttributeError: 'MyView' object has no attribute 'object' #81

derks opened this Issue Aug 16, 2012 · 3 comments


None yet
4 participants

derks commented Aug 16, 2012

I'm getting the following traceback from a view using guardian.mixins.PermissionRequiredMixin:

Traceback (most recent call last):
  File "/home/derks/env/kserver2/lib/python2.7/site-packages/django/test/", line 439, in get
    response = super(Client, self).get(path, data=data, **extra)
  File "/home/derks/env/kserver2/lib/python2.7/site-packages/django/test/", line 244, in get
    return self.request(**r)
  File "/home/derks/env/kserver2/lib/python2.7/site-packages/django/core/handlers/", line 111, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/home/derks/env/kserver2/lib/python2.7/site-packages/django/views/generic/", line 48, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/derks/env/kserver2/lib/python2.7/site-packages/guardian/", line 169, in dispatch
    response = self.check_permissions(request)
  File "/home/derks/env/kserver2/lib/python2.7/site-packages/guardian/", line 143, in check_permissions
    'object') or None
AttributeError: 'MyView' object has no attribute 'object'

The view looks like:

from django.views.generic import TemplateView
from guardian.mixins import PermissionRequiredMixin
from myapp import db

class MyView(PermissionRequiredMixin, TemplateView):
    template_name = 'myapp/myview.html'    
    model = db.MyModel
    permission_required = 'myapp.change_mymodel'

    def get_context_data(self, **kw):
        context = super(MyView, self).get_context_data(**kw)
        context['resources'] = self.model.objects.all()
        return context

The offending line in guardian.mixins.PermissionRequiredMixin is 143 which is:

obj = hasattr(self, 'get_object') and self.get_object() or getattr(self, 'object') or None

My thought is, that the following will resolve this issue (but I haven't tested it yet):

obj = hasattr(self, 'get_object') and self.get_object() or getattr(self, 'object', None)

By using getattr(self, "<attr>", <fallback_value>). I'll pull git and try this now... hopefully with a pull request.

I tried the version with de DeleteView, and I have the same error.
The only way I found for the moment to solve this issue was to override dispatch method of my VersionDeleteView class, like this:

class VersionDeleteView(LoginRequiredMixin, PermissionRequiredMixin, DeleteView):
    model = Version
    success_url = '/'
    permission_required = 'works.change_version'
    return_403 = True

    # These 2  functions below shouldn't be necessary
    # The problem is that using PermissionRequiredMixin with DeleteView give 
    # a mixin pb : l143 in guardian 1.0.5 "VersionDeleteView' object has no attribute 'kwargs'"
    def dispatch(self, request, *args, **kwargs): = kwargs.get('pk')
        return super(VersionDeleteView, self).dispatch(request, *args, **kwargs)

    def get_object(self, queryset=None):
        return Version.objects.get(

yomguy commented Jan 16, 2013

Good catch ouhouhsami!
Your solution helped me for another pb : accessing the object inside the dispatch method, like:

    def get_object(self, queryset=None):
        return Media.objects.get(

    def dispatch(self, *args, **kwargs): = kwargs.get('pk')
        media = self.get_object()
        return super(SeminarMediaView, self).dispatch(*args, **kwargs)

thanks ;)


lukaszb commented Feb 22, 2013

This was resolved recently. Please let me know if there are other problems related to this issue.

lukaszb closed this Feb 22, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment