Thursday, September 10, 2009

Add a Group to a Django User

How to add a group programatically:
from django.contrib.auth.models import Group
...

# This adds a group to the group list

g = Group()
g.name="foo"
g.save()

# To add the group to a users group list

g = Group.objects.get(name="foo")
u = MyUser.objects.get(username="foo_user")
u.groups.add(g)
u.save()

# see the groups in u
u.groups.all()

No comments:

Post a Comment