سؤال

I have some problems with django guardian. I have a news model defined

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')))

Then I try to assign the view_news permission to one of my users, and I get the following error:

>>> 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.

I have tried migrating my app but that doesn't seem to solve my problem. Any help please?

Thanks!

هل كانت مفيدة؟

المحلول

try doing:

python manage.py syncdb --all 

or make your own migration to handle the new permission (there is a bug with south that prevents autocreation of migrations for guardian permissions)

نصائح أخرى

I believe syncdb didn't the trick here. I suppose your code started to work when you added more permission to the list.

There's a error with this line:

permissions = (('view_news', _('view news')))

It should be:

permissions = (('view_news', _('view news')),)

Note the missing comma. Permissions is a tuple of tuples that are pairs of kind (permission code, permission description)

edit:

I fell into the same trap once. The best way to avoid it is organize the code as follow even with one permission:

permissions = (
    ('view_news', _('view news')),
)

After that remember to do a syncdb you as mentioned by @Foo Party and @sogeking

$ python manage.py syncdb
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top