django-permission == 0.8.0,readthedocs中的代码不起作用?

时间:2014-06-11 21:00:07

标签: python django

我正在关注readthedocs(http://django-permission.readthedocs.org/en/latest/)中的代码。难度从文档的应用权限逻辑部分开始。一切正常,因为我剪切和粘贴

art1 = Article.objects.create(
title="Article 1",
body="foobar hogehoge",
author=user1
)

生成以下追溯

Traceback (most recent call last):
File "<console>", line 4, in <module>
File "C:\Django\test_permissions\lib\site-packages\django\db\models\manager.py", line 157, in create
return self.get_queryset().create(**kwargs)
File "C:\Django\test_permissions\lib\site-packages\django\db\models\query.py", line 320, in create
obj = self.model(**kwargs)
File "C:\Django\test_permissions\lib\site-packages\django\db\models\base.py", line 417, in __init__
raise TypeError("'%s' is an invalid keyword argument for this function" % list(kwargs)[0])
TypeError: 'author' is an invalid keyword argument for this function

如果更改为

art1 = Project.objects.create(
    rest of code is okay

没关系。所以我猜错了。也许,我不确定。

无论如何,在我到达

之前,仍然会切入并粘贴到外壳中
>>> assert user1.has_perm('permission.change_article') == False
Traceback (most recent call last):
File "<console>", line 1, in <module>
AssertionError

所以我试试

>>> assert user1.has_perm('permission.change_article') == True

工作正常。我不得不说在这个阶段我不知道发生了什么。

所以下一行是

assert user1.has_perm('permission.change_article', art1) == True

现在追溯

   >>> assert user1.has_perm('permission.change_article', art1) == True
   Traceback (most recent call last):
   File "<console>", line 1, in <module>
   File "c:\django\test_permissions\lib\site-packages\django\contrib\auth\models.py", line 336, in has_perm return _user_has_perm(self, perm, obj)
   File "c:\django\test_permissions\lib\site-packages\django\contrib\auth\models.py", line 273, in _user_has_perm if backend.has_perm(user, perm, obj):
   File "c:\django\test_permissions\lib\site-packages\permission\backends.py", line 71, in has_perm if handler.has_perm(user_obj, perm, obj=obj):
   File "c:\django\test_permissions\lib\site-packages\permission\handlers.py", line 237, in has_perm if permission_logic.has_perm(user_obj, perm, obj):
   File "c:\django\test_permissions\lib\site-packages\permission\logics\author.py", line 122, in has_perm author = field_lookup(obj, self.field_name)
   File "c:\django\test_permissions\lib\site-packages\permission\utils\field_lookup.py", line 42, in field_lookup return field_lookup(field_lookup(obj, field_path[0]), field_path[1])
   File "c:\django\test_permissions\lib\site-packages\permission\utils\field_lookup.py", line 41, in field_lookup return getattr(obj, field_path[0])
   AttributeError: 'Project' object has no attribute 'project' 

我做错了吗? 我不知道该怎么做。我和Bishops Rock Lighthouse一样远; - )

我需要获得为我的项目工作的权限。这是应用程序吗?

顺便说一句。

  (test_permissions) c:\django\test_permissions\test_permissions>pip freeze
    Django==1.6.5
    Pillow==2.2.2
    South==0.8.4
    app-version==0.1.2
    django-appconf==0.6
    django-crispy-forms==1.4.0
    django-permission==0.8.0
    six==1.7.0
    tolerance==0.1.1

在Win7上使用virtualenv

托米。

1 个答案:

答案 0 :(得分:0)

我是这个图书馆的作者。

我真的很抱歉,但示例代码错误导致。如果您按照说明“从django-permission version 0.8.0开始,可以使用field__name属性指定相关对象,如django queryset lookup。请参阅下面的工作示例:”,Article没有{{ 1}}这就是为什么django责怪你的代码。它应该像

author

那么这个例子应该可行。 我会尽快更新文件。