Skip to main content

django-guardian support for Django REST Framework

Project description

django-rest-framework-guardian

Build Status Codecov Version License

django-rest-framework-guardian provides django-guardian integrations for Django REST Framework. Currently, this is only includes the DjangoObjectPermissionsFilter.

Installation & Setup

To use django-rest-framework-guardian, install it into your environment.

$ pip install djangorestframework-guardian

Ensure both Django REST Framework and django-guardian are configured and added to your INSTALLED_APPS setting.

INSTALLED_APPS = [
    'rest_framework',
    'guardian',
]

AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
    'guardian.backends.ObjectPermissionBackend',
)

DjangoObjectPermissionsFilter

The filter will ensure that querysets only returns objects for which the user has the appropriate view permission.

If you're using DjangoObjectPermissionsFilter, you'll probably also want to add an appropriate object permissions class, to ensure that users can only operate on instances if they have the appropriate object permissions. The easiest way to do this is to subclass DjangoObjectPermissions and add 'view' permissions to the perms_map attribute.

An example using both DjangoObjectPermissionsFilter and DjangoObjectPermissions might look like the following:

permissions.py:

from rest_framework import permissions


class CustomObjectPermissions(permissions.DjangoObjectPermissions):
    """
    Similar to `DjangoObjectPermissions`, but adding 'view' permissions.
    """
    perms_map = {
        'GET': ['%(app_label)s.view_%(model_name)s'],
        'OPTIONS': ['%(app_label)s.view_%(model_name)s'],
        'HEAD': ['%(app_label)s.view_%(model_name)s'],
        'POST': ['%(app_label)s.add_%(model_name)s'],
        'PUT': ['%(app_label)s.change_%(model_name)s'],
        'PATCH': ['%(app_label)s.change_%(model_name)s'],
        'DELETE': ['%(app_label)s.delete_%(model_name)s'],
    }

views.py:

from rest_framework import viewsets
from rest_framework_guardian import filters

from myapp.models import Event
from myapp.permissions import CustomObjectPermissions
from myapp.serializers import EventSerializer


class EventViewSet(viewsets.ModelViewSet):
    """
    Viewset that only lists events if user has 'view' permissions, and only
    allows operations on individual events if user has appropriate 'view', 'add',
    'change' or 'delete' permissions.
    """
    queryset = Event.objects.all()
    serializer_class = EventSerializer
    permission_classes = (CustomObjectPermissions,)
    filter_backends = (filters.DjangoObjectPermissionsFilter,)

For more information on adding 'view' permissions for models, see the relevant section of the django-guardian documentation, and this blogpost.

Release Process

  • Update changelog
  • Update package version in setup.py
  • Create git tag for version
  • Build & upload release to PyPI
    $ pip install -U pip setuptools wheel twine
    $ rm -rf dist/ build/
    $ python setup.py bdist_wheel
    $ twine upload dist/*
    

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

djangorestframework_guardian-0.1.1-py2.py3-none-any.whl (4.8 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file djangorestframework_guardian-0.1.1-py2.py3-none-any.whl.

File metadata

  • Download URL: djangorestframework_guardian-0.1.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.0

File hashes

Hashes for djangorestframework_guardian-0.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 32d723a5c62f75b72c618312358b1c186ec1c1fa95ffc2169e125df62ef20015
MD5 06e1d637943a439368d814ec7aa5821b
BLAKE2b-256 bfb8c369c08637f32523f05560f48381cb19f1f2a3dceeb68ff4f33508c39400

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page