Previous Page Next Page

Adding Groups

Django provides a django.contrib.auth.models.Group object that can be added to the groups field of a User object. The Group object has two fields—name and permissions. When you set the permissions attribute of the Group object, the permissions flow to each User who is a member of the group.

You can create a Group object using the following code:

from django.contrib.auth.models import Group
newGroup = Group()
newGroup.name = 'New Group'
newGroup.save()

Group objects can be added to and removed from a User object using the add(), remove(), and clear() functions. The add() and remove() functions accept one or more groups and then either add them to or remove them from the User object. The clear() function removes all the groups from the User object. The following code shows an example of the add(), remove(), and clear() functions:

user.groups.add(groupA, groupB)
user.groups.remove(groupC, groupD)
user.groups.clear()

Group objects can also be assigned to User objects in the admin interface. The User object details page lists groups that exist in the site. If you select groups in the groups list, the Group objects are added to the User.

The Group objects that belong to a user can be accessed through the groups attribute of the User object, as shown in the following line of code:

userGroups = user.groups

Group objects are the best way to assign permissions to several users at the same time. They are also a great way to collect users under a single label.

Try It Yourself: Create a Group and Assign Users to It

In this section, you will create a new Group called iFriends and modify the create_user() view function to automatically assign new users who are created to that group. This allows you to quickly assign permissions to all users who register with the site.

1.
Access the Django admin interface using the following URL:

http://127.0.0.1:8000/admin/

2.
Click the Groups link to bring up the group list, as shown in Figure 14.4.

Figure 14.4. The Group List view in the admin interface.


3.
Click the Add group link, shown in Figure 14.4, to bring up the Add group window, shown in Figure 14.5.

Figure 14.5. The Group Add view in the admin interface.


4.
Enter the name iFriends into the Name field, and click the Save button. The iFriends group should now appear in the group list.

5.
Open the iFriends/Home/views.py file in an editor.

6.
Modify the following import statement to include the Group object:

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

7.
Add the following line of code before user.save(), shown in Listing 14.2, to add the iFriends Group object to the User object before saving it:

user.groups.add(Group.objects.get(name='iFriends'))
user.save()

8.
Save the iFriends/Home/views.py file.

9.
Create a new User, using the following URL, as discussed in the preceding "Try It Yourself" section:

http://127.0.0.1:8000/NewUser/

10.
Access the User details in the admin interface, as discussed in the preceding section, and verify that the iFriends group is selected in the Groups list for the User object, as shown in Figure 14.6.

Figure 14.6. The User Details view in the admin interface, showing that the iFriends group was added to the new user.



Previous Page Next Page