| 1437 | Programmatically creating permissions |
| 1438 | ------------------------------------- |
| 1439 | |
| 1440 | While custom permissions can be defined within a model's ``Meta`` class, you |
| 1441 | can also create permissions directly. For example, you can create the ``can_publish`` |
| 1442 | permission for a ``BlogPost`` model in ``myapp``:: |
| 1443 | |
| 1444 | from django.contrib.auth.models import Group, Permission |
| 1445 | from django.contrib.contenttypes.models import ContentType |
| 1446 | |
| 1447 | content_type = ContentType.objects.get(app_label='myapp', model='BlogPost') |
| 1448 | perm = Permission.objects.create(codename='can_publish', name='Can Publish Posts', |
| 1449 | content_type=content_type) |
| 1450 | |
| 1451 | The permission can be assigned to a :class:`~django.contrib.auth.models.User` via |
| 1452 | ``user_permissions`` or a :class:`~django.contrib.auth.models.Group` via |
| 1453 | ``permissions``. |
| 1454 | |
| 1455 | |
| 1548 | API reference |
| 1549 | ------------- |
| 1550 | |
| 1551 | .. class:: models.Group |
| 1552 | |
| 1553 | As with both users and permissions, groups are implemented in a Django model |
| 1554 | that lives in `django/contrib/auth/models.py`_. |
| 1555 | |
| 1556 | .. _django/contrib/auth/models.py: http://code.djangoproject.com/browser/django/trunk/django/contrib/auth/models.py |
| 1557 | |
| 1558 | Fields |
| 1559 | ~~~~~~ |
| 1560 | |
| 1561 | :class:`~django.contrib.auth.models.Group` objects have the following fields: |
| 1562 | |
| 1563 | .. attribute:: models.Group.name |
| 1564 | |
| 1565 | Required. 80 characters or fewer. Any characters are permitted. Example: |
| 1566 | ``'Awesome Users'``. |
| 1567 | |
| 1568 | .. attribute:: models.Group.permissions |
| 1569 | |
| 1570 | Many-to-many field to :class:`~django.contrib.auth.models.Permissions`:: |
| 1571 | |
| 1572 | group.permissions = [permission_list] |
| 1573 | group.permissions.add(permission, permission, ...) |
| 1574 | group.permissions.remove(permission, permission, ...) |
| 1575 | group.permissions.clear() |
| 1576 | |