7

I've got a model defined in my Django app foo which looks like this:

class Bar(models.Model):
    class Meta:
        permissions = (
            ("view_bar", "Can view bars"),
        )

I've run manage.py syncdb on this, and sure enough, it shows up in the auth_permissions table:

id|name|content_type_id|codename
41|Can view bars|12|view_bar

However, when I try adding that permission to a user object in a view, like so:

request.user.user_permissions.add('foo.view_bar')

The code blows up with the following exception:

invalid literal for int() with base 10: 'foo.view_bar'

What's going on?

Chris B.
  • 85,731
  • 25
  • 98
  • 139

1 Answers1

14

user_permissions.add is adding to a ManyToMany manager. So you need to add the actual Permission object itself:

from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType

content_type = ContentType.objects.get_for_model(Bar)
permission = Permission.objects.get(content_type=content_type, codename='view_bar')

request.user.user_permissions.add(permission)

Also, you may experience weirdness when you're testing because permissions are also cached for each user. You may want to delete the cache before you call has_perm:

if hasattr(user, '_perm_cache'):
    delattr(user, '_perm_cache')

In general, you probably want to write a bunch of helper methods that take care of all of this stuff so you can give and revoke permissions easily programatically. How you do so would really depend on how you're using the permissions.

dgel
  • 16,352
  • 8
  • 58
  • 75
  • Yeah, like I said, you probably want to write some nice helper methods that work like you would expect `user_permissions.add` to actually work. – dgel Apr 12 '12 at 20:50
  • If the `codename` is unique, can't you get the `Permission` instance without obtaining the `ContentType`? E.g. `Permission.objects.get(codename='view_bar')` seems to work in my case. – djvg Sep 04 '18 at 12:47
  • @djvg, `codename` is not unique across Models so get on that can fail with multiple records found – Michael Neylon Apr 29 '20 at 00:05
  • 1
    @MichaelNeylon: You are right, `codename` is *not necessarily* unique, as also explained e.g. here: https://stackoverflow.com/a/46561094. I should have put more emphasis on the "If." – djvg Apr 29 '20 at 08:09