Django A user in a group code example
Example 1: django user group check
from django.contrib.auth.models import User, Group
group = Group(name="Author")
group.save()
user = User.objects.get(username="Johndoe")
user.groups.add(group)
if user.groups.filter(name=group):
else:
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)
Example 3: django add user to group
from django.contrib.auth.models import Group
my_group = Group.objects.get(name='my_group_name')
my_group.user_set.add(your_user)