Skip to content

Instantly share code, notes, and snippets.

@aimedey19
Created March 6, 2023 08:12
Show Gist options
  • Save aimedey19/69916b2c164359bce70f0d1327470421 to your computer and use it in GitHub Desktop.
Save aimedey19/69916b2c164359bce70f0d1327470421 to your computer and use it in GitHub Desktop.
# Creating and getting the group
teacher_group, created = Group.objects.get_or_create(name='Teacher')
# Setting permissions
# Granting all access on CourseCategory Model
content_type_course_category = ContentType.objects.get_for_model(CourseCategory)
# Granting all access on Course Model
content_type_course = ContentType.objects.get_for_model(Course)
content_type_query = Q(content_type=content_type_course_category) | Q(content_type=content_type_course)
code_name_query = Q(codename='delete_course') | Q(codename='delete_coursecategory')
permissions = Permission.objects.filter(content_type_query).exclude(code_name_query)
teacher_group.permissions.set(permissions)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment