I added a new model with one permission, and now I need to add that permission to a few users on the production machine after deploying the code and running syncdb for the new app involved. I haven't found the correct way to do this. The auth docs mention User.user_permissions.add(permission), but never tell me what 'permission' is or the best way to get it.
Permission
(which lives in django.contrib.auth.models
) is a database object. You'll be able to see all of them with Permission.objects.all()
. They are created automatically by a post-sync signal for each model (and as the docs mention, you can also define your own).
To assign the permissions to a User, you will first have to get the Permission
objects (using Permission.objects.get(*args)
), and then you can add it to the User with User.user_permissions.add(permission)
as you mentioned.
Alternatively, and the easier way if you can do this, is just to use the Django admin site. In the detail page for each user, there is a section relating to permissions. I'm guessing you aren't using these permissions outside of the admin, so that's the only area they will affect. If you want all of your users to have all permissions, you can make them superusers by setting the is_superuser
flag on each User to True
.