I've solved this. First of all, the codename for the permission should be 
called as a codename and second, I had to remove the app name 
(display_data) like this:

permission = Permission.objects.get(codename='settings')


On Tuesday, April 9, 2019 at 9:25:09 PM UTC-6, Matt Collins wrote:
>
> Hi,
>
> I'm trying to set a permission for a user that I'm creating with a python 
> script. I've tested and found that the codename for the permission is 
> "display_data.settings" using the following:
>
> python manage.py shell
> from django.contrib.auth.models import User
> user_name = User.objects.get(username="user_name")
> user_name.get_all_permissions()
> set([u'display_data.settings']) 
>
>
> I'm trying to set this permission for the user with the following:
>
> from django.contrib.auth import get_user_model
> from django.contrib.auth.models import User, Permission
>
> User = get_user_model()
>
> #check to see if user exists, create it if it doesn't
> User.objects.filter(username="user_name").exists() or\
>     User.objects.create_user(username='user_name', email='
> [email protected]', password='blah')
>  
>
> #add permissions to user
> u = User.objects.get(username="user_name")
> permission = Permission.objects.get(name='display_data.settings')
> u.user_permissions.add(permission)
>
>  
> When I run this, I get the following error:
>
>
> django.contrib.auth.models.DoesNotExist: Permission matching query does 
> not exist.
>
>
> Can anybody tell me what I'm missing? 
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/e092d493-6dc5-4374-ad9e-e664e6b9d0b6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to