How to assign users to groups in django

How do i assign a user on successfull registration to a particular group in django

message profile
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-26
loading..

DEVMAESTERS

Newsletter

Services

Frontend Development |Backend Development |Full Website Development |Bootstrap Website upgrades | Website Debbugging | Website Hosting & deployment

Contact

Interested in hiring me or collaborating with me on a project, click on any of the links below to get my social media handle

Or contact me via Tel: (+234)-806-225-7480 | Email: abubakarzakari1703@gmail.com

Copywright@devmaesters.com
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.