How Can I Create a User Group?

8,322 views
Skip to first unread message

b14ck

unread,
Mar 2, 2010, 2:07:22 AM3/2/10
to Django users
Hi everyone,

I'm relatively new to Django, and I'm building a site which needs to
have multiple permission levels and groups. My experience with
django.contrib.auth at this point is decent.

What I'm trying to do is create a group for each service that my
company offers. For example:

- Conferencing
- Web Design
- Image Creation

(Just picking random things.) But anyhow, I can't figure out how to do
this.

The django.contrib.auth documentation shows the following group
information:

User objects have two many-to-many fields: models.User. groups and
user_permissions. User objects can access their related objects in the
same way as any other Django model:

myuser.groups = [group_list]
myuser.groups.add(group, group, ...)
myuser.groups.remove(group, group, ...)
myuser.groups.clear()
myuser.user_permissions = [permission_list]
myuser.user_permissions.add(permission, permission, ...)
myuser.user_permissions.remove(permission, permission, ...)
myuser.user_permissions.clear()

But I'm unclear on how to actually great a group object which I can
then add to my user's groups list.

Eventually I'll also want to create individual permissions (like
can_edit_page, etc.) but I'll work on that once I've got groups going.

Thanks so much for your time, I really appreciate it. I've spent a
while looking for the information but I think I'm missing something
obvious.

stherrien

unread,
Mar 2, 2010, 9:08:56 AM3/2/10
to Django users
Hi b14ck,

To create a group you must go into the admin under the Auth section
there you can create groups for each of the three models your using by
selecting the proper permissions and then go into the user edit page
and assign the group on the user you want to have that specific group.
hope this helps.


Thanks,

Shawn Therrien

Matt McCants

unread,
Mar 2, 2010, 10:18:35 AM3/2/10
to django...@googlegroups.com
To add a group from the API, you can do this:

from django.contrib.auth.models import User,Group

# Get or Create the Group (I <3 this method)
mygroup, created = Group.objects.get_or_create(name='Web Design')

# Add fakeuser to group
myuser = User.objects.get(username='fakeuser')
myuser.groups.add(mygroup)

Matt


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


MauroCam

unread,
Mar 2, 2010, 4:37:15 PM3/2/10
to Django users
Hi,

we have an init.py script that sets up all the baseline data. In it we
create the necessary groups and assign the core admin users to the
groups.

from django.contrib.auth.models import Group as DjangoGroup
gUsers = DjangoGroup(name='Users')
gUsers.save()
gGroupAdmins = DjangoGroup(name='GroupAdmins')
gGroupAdmins.save()

# Set users
zen = User.objects.create_user('zen', 'zen@emailaddress',
'pwd123')
zen.groups = [gUsers]

Hope that answers your question

Reply all
Reply to author
Forward
0 new messages