Skip to content

Instantly share code, notes, and snippets.

@Sunil-ghodela
Last active May 3, 2019 10:52
Show Gist options
  • Save Sunil-ghodela/edd10f94502698e18c5eb6191c7920e2 to your computer and use it in GitHub Desktop.
Save Sunil-ghodela/edd10f94502698e18c5eb6191c7920e2 to your computer and use it in GitHub Desktop.
Add group in User
https://stackoverflow.com/questions/6288661/adding-a-user-to-a-group-in-django
from django.contrib.auth.models import Group
group = Group.objects.get(name='groupname')
user.groups.add(group)
l = request.user.groups.values_list('name',flat=True)
user by group name
User.objects.filter(groups__name='Admin').count()
Creating new group...
groups_name_list = [A,B,C,D]
for group in groups_name_list:
c, _ =Group.objects.get_or_create(name=group)
Removing auser from group
g.user_set.remove(your_user)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment