[Answer]-My created user don't have default permission (add,delete,change)?

1👍

Only superuser has all permissions. Permissions should be added to non-superusers.

from django.contrib.auth.models import Permission

...

b.user_permissions.add(Permission.objects.get(content_type__app_label='student',
                                              codename='add_user'))
b.user_permissions.add(Permission.objects.get(content_type__app_label='student',
                                              codename='change_user'))
b.user_permissions.add(Permission.objects.get(content_type__app_label='student',
                                              codename='delete_user'))

0👍

try this:

from django.contrib.auth.models import User, Group, Permission

b = User.objects.create_user('mina','mina@dd.dd','123456') 
permission = Permission.objects.get(name='Can add user') # get permission by name
b.user_permissions.add(permission) #add permission to user
b.save()

Or you can create a group with all permissons that you want, and add user to this group like this:

group = Group.objects.get(name='wizard')
user.groups.add(group)

And take a look at this.

0👍

Looks like you are using a custom user model. Your user model needs to inherit the PermissionsMixin

from django.contrib.auth.models import PermissionsMixin

Also it’s recommended that your model inherits from AbstractBaseUser

See https://docs.djangoproject.com/en/dev/topics/auth/customizing/#specifying-a-custom-user-model

class User(AbstractBaseUser, PermissionsMixin):
    ...

Leave a comment