How to assign users to groups in django
How do i assign a user on successfull registration to a particular group in django
Admin
2022-03-26
To add a user to a group first import Group into views.py
from django.contrib.auth.models import Group
Create a variable and assign it to the group you want to add the user to
group = Group.objects.get(name='Authors')
For this example "Authors" is the group i created and want to assign my new user to
Next, create a variable and assign it to the user you just created. Ensure that this code is set to run only on successful registration of the user.
user = User.objects.get(username=username)
And finally to add the new user
user.groups.add(group)
This will add the user to the authors group returned by the group const
Add Message
Click on the button below to add a new message to this thread
Tags
Thread detail
Satus: Open
Messages: 1Started: 2022-03-26loading..
Privacy Policy
By using our website,
you agree that devmaesters can store cookies on your device and disclose information in accordance with our privacy policy.