permission based serializing for django restframework
Project description
restframework-serializer-permissions
Drop in replacement for Django Restframework Serializers to add permission based field serialization.
Installation
Install this module into your environment:
$ pip install restframework-serializer-permissions
Example
This example uses a ModelSerializer as described in DRF Docs
# import permissions from rest_framework
from rest_framework.permissions import AllowAny, IsAuthenticated
# import serializers from serializer_permissions instead of rest_framework
from serializer_permissions import serializers
# import you models
from myproject.models import ShoppingItem, ShoppingList
class ShoppingItemSerializer(serializers.ModelSerializer):
item_name = serializers.CharField()
class Meta:
# metaclass as described in drf docs
model = ShoppingItem
fields = ('item_name', )
class ShoppingListSerializer(serializers.ModelSerializer):
# Allow all users to list name
list_name = serializers.CharField(permission_classes=(AllowAny, ))
# Only allow authenticated users to retrieve the comment
list_comment = serializers.CharField(permissions=(IsAuthenticated, ))
# show owner only, when the current user has 'auth.view_user' permission
owner = serializers.CharField(permissions=('auth.view_user', ), hide=True)
# serializer which is only available, when the user is authenticated
items = ShoppingItemSerializer(many=True, permissions=(IsAuthenticated, ), hide=True)
class Meta:
# metaclass as described in drf docs
model = ShoppingItem
fields = ('list_name', 'list_comment', 'owner', 'items', )
You can also define your own permissions classes as described in the DRF documentation and specify multiple rpermission_classes
and rpermissions
on a field or serializer: all provided permissions must be satisfied for the visitor to retrieve the given field.
Compatibility
This package should be compatible with:
- Django Rest Framework 3
- Django 2.x, 3.x
- Python 3.6, 3.7, 3.8
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for restframework-serializer-permissions-0.0.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | c726b4369713c0c5dfb162ac20527a2e81281df29cb73644a8abe19f4002b5ea |
|
MD5 | 976104d47d2d2f33ce11491238b9cb78 |
|
BLAKE2b-256 | d4d47341c1ae9bad04c45e24c5f807817342f0774882b68098625ff5806e2aa5 |
Close
Hashes for restframework_serializer_permissions-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a580f3746fd6f61151d8e021457c1f785041609289b2d5a700bf7cadabd8a1f9 |
|
MD5 | 01c74bc7c2bcf78d37382d538ed417b8 |
|
BLAKE2b-256 | cb992d1b1fdcbe8f4a69c0040b018158c68860d6514b96d3d9d22390cbfb7b2c |