Sometimes, we want to add a user to a group in Python Django.
In this article, we’ll look at how to add a user to a group in Python Django.
How to add a user to a group in Python Django?
To add a user to a group in Python Django, we can use the add
method.
For instance, we write
from django.contrib.auth.models import Group
my_group = Group.objects.get(name='my_group_name')
my_group.user_set.add(your_user)
to get the group with the given name with Group.objects.get
.
And then we call user_set.add
on the returned group with the user to add the user to the group.
Conclusion
To add a user to a group in Python Django, we can use the add
method.