Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #13211 -- Added the `Group` API reference and a `Permission` AP…

…I example to the `contrib.auth` documentation. Thanks to b14ck for the report and to jpaulett and CrazyGir for the patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16849 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 9316671effabcd42c7af81cb9dac9838d07c082a 1 parent e63fa0f
Julien Phalip authored September 18, 2011

Showing 1 changed file with 46 additions and 7 deletions. Show diff stats Hide diff stats

  1. 53  docs/topics/auth.txt
53  docs/topics/auth.txt
@@ -131,7 +131,7 @@ Methods
131 131
 .. class:: models.User
132 132
 
133 133
     :class:`~django.contrib.auth.models.User` objects have two many-to-many
134  
-    fields: models.User. ``groups`` and ``user_permissions``.
  134
+    fields: ``groups`` and ``user_permissions``.
135 135
     :class:`~django.contrib.auth.models.User` objects can access their related
136 136
     objects in the same way as any other :doc:`Django model
137 137
     </topics/db/models>`:
@@ -1403,12 +1403,7 @@ API reference
1403 1403
 
1404 1404
 .. currentmodule:: django.contrib.auth.models
1405 1405
 
1406  
-.. class:: Permission
1407  
-
1408  
-    Just like users, permissions are implemented in a Django model that lives
1409  
-    in `django/contrib/auth/models.py`_.
1410  
-
1411  
-.. _django/contrib/auth/models.py: http://code.djangoproject.com/browser/django/trunk/django/contrib/auth/models.py
  1406
+.. class:: models.Permission
1412 1407
 
1413 1408
 Fields
1414 1409
 ~~~~~~
@@ -1437,6 +1432,26 @@ data-access methods like any other :doc:`Django model </ref/models/instances>`.
1437 1432
 
1438 1433
 .. currentmodule:: django.contrib.auth
1439 1434
 
  1435
+Programmatically creating permissions
  1436
+-------------------------------------
  1437
+
  1438
+While custom permissions can be defined within a model's ``Meta`` class, you
  1439
+can also create permissions directly. For example, you can create the
  1440
+``can_publish`` permission for a ``BlogPost`` model in ``myapp``::
  1441
+
  1442
+    from django.contrib.auth.models import Group, Permission
  1443
+    from django.contrib.contenttypes.models import ContentType
  1444
+
  1445
+    content_type = ContentType.objects.get(app_label='myapp', model='BlogPost')
  1446
+    permission = Permission.objects.create(codename='can_publish',
  1447
+                                           name='Can Publish Posts',
  1448
+                                           content_type=content_type)
  1449
+
  1450
+The permission can then be assigned to a
  1451
+:class:`~django.contrib.auth.models.User` via its ``user_permissions``
  1452
+attribute or to a :class:`~django.contrib.auth.models.Group` via its
  1453
+``permissions`` attribute.
  1454
+
1440 1455
 Authentication data in templates
1441 1456
 ================================
1442 1457
 
@@ -1529,6 +1544,30 @@ group ``'Special users'``, and you could write code that could, say, give them
1529 1544
 access to a members-only portion of your site, or send them members-only email
1530 1545
 messages.
1531 1546
 
  1547
+API reference
  1548
+-------------
  1549
+
  1550
+.. class:: models.Group
  1551
+
  1552
+Fields
  1553
+~~~~~~
  1554
+
  1555
+:class:`~django.contrib.auth.models.Group` objects have the following fields:
  1556
+
  1557
+.. attribute:: Group.name
  1558
+
  1559
+    Required. 80 characters or fewer. Any characters are permitted. Example:
  1560
+    ``'Awesome Users'``.
  1561
+
  1562
+.. attribute:: Group.permissions
  1563
+
  1564
+    Many-to-many field to :class:`~django.contrib.auth.models.Permissions`::
  1565
+
  1566
+        group.permissions = [permission_list]
  1567
+        group.permissions.add(permission, permission, ...)
  1568
+        group.permissions.remove(permission, permission, ...)
  1569
+        group.permissions.clear()
  1570
+
1532 1571
 .. _authentication-backends:
1533 1572
 
1534 1573
 Other authentication sources

0 notes on commit 9316671

Please sign in to comment.
Something went wrong with that request. Please try again.