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.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | a0b7baee9288d6d891d1fcef7d60346d056e037b6c8727963f0f0081442f955f |
|
MD5 | 716173f6f92366d63e4ee8e3b9c31425 |
|
BLAKE2b-256 | ba7f1c54fddf73a47fa08e014af24d91458d3c2fca12b7444ed2411341e3b2d9 |
Close
Hashes for restframework_serializer_permissions-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 72d26850ad9cf45a8705d5c97c5ecfdc10eb1be6f0a6e321fdca7bb97e383024 |
|
MD5 | 30c33c797b72d576e53a06bbc22baae9 |
|
BLAKE2b-256 | c4caa343e032d50bf5afef4fb0cc8a5f482dc929c14a4b1694427bd7e289cd77 |