Reza Khalafi
Django permissions and group learning.
1.Import dependencies:
from django.contrib.auth.models import User, Group, Permission from django.contrib.contenttypes.models import ContentType
2.Create group:
Group.objects.create(name='wizard')
3.Get content_type:
content_type = ContentType.objects.get(app_label='myapp', model='blogpost')
4.Create permission:
permission = Permission.objects.create(codename='can_publish', name='Can Publish Posts', content_type=content_type)
5.Get user:
user = User.objects.get(username='reza7')
6.Get permission:
content_type = ContentType.objects.get(app_label='myapp', model='blogpost') permission = Permission.objects.get(codename='add_blogpost', content_type=content_type)
7.Get group:
group = Group.objects.get(name='wizard')
8.Assign permission to group:
group.permissions.add(permission)
9.Assign user to groups:
user.groups.add(group)
10.Check user has permission:
user.has_perm('myapp.can_publish')