我的Django项目中有两个应用程序,让我们假设它是appA和appB。这些应用程序通过pip install
从外部包安装。它们在各自的模型中都有额外的权限:
appA has got:
class Meta:
permissions = (
('clone_poll', 'Can clone poll'),
('close_poll', 'Can close poll'),
('viewresults_poll', 'Can view poll results'),
)
appB得到了:
class Meta:
permissions = (
('viewresults_meeting', 'Can view meeting results'),
)
我在我的项目中安装了这两个应用程序,并试图创建它们所拥有的模型的一些实例。一切正常(对象被创建并保存到DB),直到权限分配步骤,其中发生以下错误:
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'
我检查了发生错误的确切位置,它在django-guardian内部。垃圾收集器似乎清理了本地上下文,将所有导入设置为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
更重要的是,当我从INSTALLED_APPS中删除其中一个应用程序时,例如appA,其他应用程序工作正常,即设置了所有权限。
我不知道这个问题的根源是什么,如果它是在我的应用程序,在监护人或完全在其他应用程序?
经过几天的努力,我终于找到了解决这个问题的方法,以及这个问题的真正根源。首先,我一直在使用Django 1.6.10。在这两个应用程序中,我都为user_logged_in信号注册了处理程序。我将适当的函数和绑定(user_logged_in.connect(handler))放入两个应用程序中称为signals.py的文件中。然后在__init__.py中导入signals.py,以使信号连接工作。在其中一个文件中还有另一个依赖于models.py的文件,它也被导入到signals.py中——这是在其中一个应用中完成的。说到这一点,问题是在一个应用程序中的循环导入。我发现工作的解决方案是将signals.py内容移动到models.py,从__init__.py中删除signals.py .