Search code examples
pythondjangodjango-modelspermissionsdjango-guardian

Working with Django Model Permission raises DoesNotExist error


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!


Solution

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