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

Closed
derks opened this Issue Aug 16, 2012 · 3 comments

Comments

Projects
None yet
4 participants
Contributor

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/client.py", 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/client.py", line 244, in get
    return self.request(**r)
  File "/home/derks/env/kserver2/lib/python2.7/site-packages/django/core/handlers/base.py", 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/base.py", line 48, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/derks/env/kserver2/lib/python2.7/site-packages/guardian/mixins.py", line 169, in dispatch
    response = self.check_permissions(request)
  File "/home/derks/env/kserver2/lib/python2.7/site-packages/guardian/mixins.py", 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 mixins.py "VersionDeleteView' object has no attribute 'kwargs'"
    def dispatch(self, request, *args, **kwargs):
        self.pk = kwargs.get('pk')
        return super(VersionDeleteView, self).dispatch(request, *args, **kwargs)

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

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(id=self.pk)

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

thanks ;)
A+

Contributor

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