2

I am attempting to assign a user permissions to a task using django-guardian and I'm getting errors.

Inside of my Task class in task/models.py, I have:

class Meta(BaseModel.Meta):
    permissions = (
        ("task_view", "Person can view task details"),
    )

When I go into

python manage.py shell

and attempt to assign those permissions, it throws an error.

Here is what I'm attempting:

from profiles.models import SiteUser
from task.models import *
from guardian.shortcuts import assign_perm
siteuser = SiteUser.objects.get(id=2454)
task = Task.objects.get(name="asdf")
assign_perm('task_view', siteuser, task)

And here's the error:

DoesNotExist: Permission matching query does not exist.

Does anyone know what I'm doing wrong? I think I followed the instructions in the django-guardian help, but maybe I missed something?

Foo Party
  • 596
  • 1
  • 4
  • 13

2 Answers2

2

OK, it looks like you have to do a syncdb before you can assign the permission:

python manage.py syncdb --all
Foo Party
  • 596
  • 1
  • 4
  • 13
  • 1
    You can also use [django-extensions](http://django-extensions.readthedocs.org/en/latest/)'s `update_permissions` management command. – gregoltsov Aug 07 '14 at 14:24
0

Following Gregory Goltsov comment, with Django 1.8 i ended up calling django-extensions update_permissions programatically that i call when i do a post_migration

from django.core import management
from guardian.shortcuts import assign_perm
from django.contrib.auth.models import Permission, Group

def create_user_groups(sender, **kwargs):
    management.call_command('update_permissions')
    group, created = Group.objects.get_or_create(name='personal')
    if created:
        assign_perm('core.view_savedlocation', group)

    group, created = Group.objects.get_or_create(name='business')
    if created:
        assign_perm('core.add_savedlocation', group)
        assign_perm('core.change_savedlocation', group)
        assign_perm('core.view_savedlocation', group)
        assign_perm('core.delete_savedlocation', group)

This way up to this point i have all permissions i need before assigning them to the groups i want.

psychok7
  • 5,373
  • 9
  • 63
  • 101