Marius
Marius

Reputation: 86

Assiging permissions with guardian's shortcut assign_perm results in 'NoneType' object has no attribute 'objects'

I have two applications in my Django project, let's assume it's appA and appB. Those applications are installed from external packages via pip install. Both of them have additional permissions in their models:

appA has got:

  class Meta:                                                                  
      permissions = ( 
          ('clone_poll', 'Can clone poll'),                                    
          ('close_poll', 'Can close poll'),            
          ('viewresults_poll', 'Can view poll results'),                         
)  

appB has got:

  class Meta:                                                                  
      permissions = ( 
          ('viewresults_meeting', 'Can view meeting results'),                         
)  

I installed both applications in my project and tried to create some instances of models their have. Everything works fine(object is created and saved to DB) until permission assign step, where the following error occurred:

Traceback (most recent call last):
  File "/home/polls/pythonPolls/lib/python2.7/site-packages/django/core/handlers/base.py", line 114, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/polls/pythonPolls/lib/python2.7/site-packages/django/contrib/auth/decorators.py", line 22, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/home/polls/pythonPolls/lib/python2.7/site-packages/polls/views.py", line 529, in clone_poll
    initial_dict=initial)(request, kwargs=kwa)
  File "/home/polls/pythonPolls/lib/python2.7/site-packages/django/views/generic/base.py", line 69, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/polls/pythonPolls/lib/python2.7/site-packages/django/contrib/formtools/wizard/views.py", line 236, in dispatch
    response = super(WizardView, self).dispatch(request, *args, **kwargs)
  File "/home/polls/pythonPolls/lib/python2.7/site-packages/django/views/generic/base.py", line 87, in dispatch
    return handler(request, *args, **kwargs)
  File "/home/polls/pythonPolls/lib/python2.7/site-packages/django/contrib/formtools/wizard/views.py", line 297, in post
    return self.render_done(form, **kwargs)
  File "/home/polls/pythonPolls/lib/python2.7/site-packages/django/contrib/formtools/wizard/views.py", line 350, in render_done
    done_response = self.done(final_form_list, **kwargs)
  File "/home/polls/pythonPolls/lib/python2.7/site-packages/polls/views.py", line 203, in done
    assign_perm('polls.change_poll', self.request.user, poll_obj)
  File "/home/polls/pythonPolls/lib/python2.7/site-packages/guardian/shortcuts.py", line 91, in assign_perm
    return model.objects.assign_perm(perm, user, obj)
  File "/home/polls/pythonPolls/lib/python2.7/site-packages/guardian/managers.py", line 41, in assign_perm
    obj_perm, created = self.get_or_create(**kwargs)
  File "/home/polls/pythonPolls/lib/python2.7/site-packages/django/db/models/manager.py", line 154, in get_or_create
    return self.get_queryset().get_or_create(**kwargs)
  File "/home/polls/pythonPolls/lib/python2.7/site-packages/django/db/models/query.py", line 380, in get_or_create
    obj.save(force_insert=True, using=self.db)
  File "/home/polls/pythonPolls/lib/python2.7/site-packages/guardian/models.py", line 44, in save
    content_type = ContentType.objects.get_for_model(self.content_object)
AttributeError: 'NoneType' object has no attribute 'objects'

I inspected the exact place of occurring the error, it is in django-guardian internal. It seems that garbage collector cleaned local context, setting all imports to None:

'UserObjectPermissionManager': None, 
'__file__': None, 
'user_model_label': None, 
'__name__': None, 
'GroupObjectPermissionManager': None, 
'ContentType': None,
'settings': None, 
'Permission': None, 
'GroupObjectPermissionBase': None, 
'BaseObjectPermission': None,  
'UserObjectPermissionBase': None

What is more, when I remove one of the applications e.g. appA from INSTALLED_APPS, the other works fine i.e. all permissions are set.

I have no idea what is the origin of this problem, if it's in my apps, in guardian or in completely other application?

Upvotes: 0

Views: 379

Answers (1)

Marius
Marius

Reputation: 86

After a few days of struggling with this problem I finally found the solution, and the real origin of this problem. First of all I've been using Django 1.6.10. In both applications I have registered handlers for user_logged_in signals. I put the proper functions and bindings(user_logged_in.connect(handler)) into the file called signals.py in both apps. Then signals.py was imported in __init__.py, to make the signals connection work. There was also another dependance on the models.py in one of the files, which was also imported into signals.py- this was done in one of the apps. Getting to the point, the problem was circular imports in one of the app. The solution I found to work was to move signals.py contents to models.py, remove signals imports from __init__.py.

Upvotes: 1

Related Questions