Answers for "get user group djangoi"

3

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
Posted by: Guest on October-13-2020
-1

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)
Posted by: Guest on May-03-2020

Python Answers by Framework

Browse Popular Code Answers by Language