我对django监护人有些问题。我有一个新闻模型定义
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.
我尝试迁移我的应用,但这似乎无法解决我的问题。有什么帮助吗?
谢谢!
答案 0 :(得分:5)
尝试做:
python manage.py syncdb --all
或进行自己的迁移以处理新权限(南方存在一个错误,阻止自动创建迁移以获得监护人权限)
答案 1 :(得分:1)
我相信syncdb不是这里的诀窍。我想当你向列表中添加更多权限时,你的代码就开始工作了。
此行存在错误:
permissions = (('view_news', _('view news')))
应该是:
permissions = (('view_news', _('view news')),)
请注意缺少的逗号。权限是一对元组的元组(权限代码,权限描述)
修改强>:
我曾陷入同一陷阱。避免它的最佳方法是使用一个权限组织代码如下:
permissions = (
('view_news', _('view news')),
)
之后记得按照@Foo Party和@sogeking
的提法做一个syncdb$ python manage.py syncdb