get django user group code example

Example 1: django user group check

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

group = Group(name="Author")
group.save() # Create a sample group.
user = User.objects.get(username="Johndoe") # get Some User.
user.groups.add(group) # Add User 'Johndoe' to a Group.

# check if user belongs to certain group.
if user.groups.filter(name=group):
  # do your stuff and give user access.
else:
  # redirect him

Example 2: django add user to group

from django.contrib.auth.models import Group, User
tonystark=User.objects.get(id=pk)
hulk=Group.objects.get(name='groupname')
hulk.user_set.add(tonystark)