我对django guardian有一些问题。我定义了新闻模型
class News(models.Model):
title = models.CharField(_('Title'), max_length=255)
slug = models.SlugField(_('Slug'), unique_for_date='pub_date',)
objects = models.Manager()
featured = FeaturedNewsManager()
link = models.URLField(_('Link'), blank=True, null=True,)
class Meta:
permissions = (('view_news', _('view news')))
然后,我尝试将view_news权限分配给我的一个用户,并收到以下错误:
>>> from guardian.shortcuts import assign_perm
>>> g = Group.objects.latest('pk')
>>> n = News.objects.get(pk=4)
>>> assign_perm( 'news.view_news', g, n)
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/home/roberto/.virtualenvs/ve_news/local/lib/python2.7/site-packages/guardian/shortcuts.py", line 93, in assign_perm
return model.objects.assign_perm(perm, group, obj)
File "/home/roberto/.virtualenvs/ve_news/local/lib/python2.7/site-packages/guardian/managers.py", line 90, in assign_perm
permission = Permission.objects.get(content_type=ctype, codename=perm)
File "/home/roberto/.virtualenvs/ve_news/local/lib/python2.7/site-packages/django/db/models/manager.py", line 143, in get
return self.get_query_set().get(*args, **kwargs)
File "/home/roberto/.virtualenvs/ve_news/local/lib/python2.7/site-packages/django/db/models/query.py", line 404, in get
self.model._meta.object_name)
DoesNotExist: Permission matching query does not exist.
我尝试迁移我的应用程序,但这似乎不能解决我的问题。请帮忙吗?
谢谢!
尝试执行以下操作:
python manage.py syncdb --all
或者进行自己的迁移来处理新权限(South 存在一个错误,阻止自动创建保护者权限的迁移(
我相信
syncdb在这里没有技巧。我想当您向列表添加更多权限时,您的代码开始工作。
此行存在错误:
permissions = (('view_news', _('view news')))
它应该是:
permissions = (('view_news', _('view news')),)
请注意缺少的逗号。权限是成对的元组(权限代码、权限说明(
编辑:
我曾经落入过同样的陷阱。避免它的最佳方法是即使具有一个权限,也可以按如下方式组织代码:
permissions = (
('view_news', _('view news')),
)
之后记得按照@Foo方和@sogeking
$ python manage.py syncdb