Forms — django-guardian v1.0.0.beta1 documentation

### Navigation

- index
- modules |
- next |
- previous |
- django-guardian v1.0.0.beta1 documentation »
- API Reference »

# Forms

## UserObjectPermissionsForm

class `guardian.forms.` `UserObjectPermissionsForm`(user, *args, **kwargs)

  Bases: `guardian.forms.BaseObjectPermissionsForm`

  Object level permissions management form for usage with `User` instances.

  Example usage:

      from django.contrib.auth.models import User
      from django.shortcuts import get_object_or_404
      from myapp.models import Post
      from guardian.forms import UserObjectPermissionsForm

      def my_view(request, post_slug, user_id):
          user = get_object_or_404(User, id=user_id)
          post = get_object_or_404(Post, slug=post_slug)
          form = UserObjectPermissionsForm(user, post, request.POST or None)
          if request.method == 'POST' and form.is_valid():
              form.save_obj_perms()
          ...

  `save_obj_perms`()

    Saves selected object permissions by creating new ones and removing those which were not selected but already exists.

    Should be called after form is validated.

## GroupObjectPermissionsForm

class `guardian.forms.` `GroupObjectPermissionsForm`(group, *args, **kwargs)

  Bases: `guardian.forms.BaseObjectPermissionsForm`

  Object level permissions management form for usage with `Group` instances.

  Example usage:

      from django.contrib.auth.models import Group
      from django.shortcuts import get_object_or_404
      from myapp.models import Post
      from guardian.forms import GroupObjectPermissionsForm

      def my_view(request, post_slug, group_id):
          group = get_object_or_404(Group, id=group_id)
          post = get_object_or_404(Post, slug=post_slug)
          form = GroupObjectPermissionsForm(group, post, request.POST or None)
          if request.method == 'POST' and form.is_valid():
              form.save_obj_perms()
          ...

  `save_obj_perms`()

    Saves selected object permissions by creating new ones and removing those which were not selected but already exists.

    Should be called after form is validated.

## BaseObjectPermissionsForm

class `guardian.forms.` `BaseObjectPermissionsForm`(obj, *args, **kwargs)

  Base form for object permissions management. Needs to be extended for usage with users and/or groups.

  Parameters: | obj – Any instance which form would use to manage object permissions”

  `are_obj_perms_required`()

    Indicates if at least one object permission should be required. Default: `False`.

  `get_obj_perms_field`()

    Returns field instance for object permissions management. May be replaced entirely.

  `get_obj_perms_field_choices`()

    Returns choices for object permissions management field. Default: list of tuples `(codename, name)` for each `Permission` instance for the managed object.

  `get_obj_perms_field_class`()

    Returns object permissions management field’s base class. Default: `django.forms.MultipleChoiceField`.

  `get_obj_perms_field_initial`()

    Returns initial object permissions management field choices. Default: `[]` (empty list).

  `get_obj_perms_field_label`()

    Returns label of the object permissions management field. Defualt: `_("Permissions")` (marked to be translated).

  `get_obj_perms_field_name`()

    Returns name of the object permissions management field. Default: `permission`.

  `get_obj_perms_field_widget`()

    Returns object permissions management field’s widget base class. Default: `django.forms.SelectMultiple`.

  `save_obj_perms`()

    Must be implemented in concrete form class. This method should store selected object permissions.

### Navigation

- index
- modules |
- next |
- previous |
- django-guardian v1.0.0.beta1 documentation »
- API Reference »