Permissions for django-rest fields
Project description
Overview
This package provides a mixin for setting permissions to separate fields of ModelSerializer which uses DRF permission classes. You can hide or set a field as read-only for specific users.
Requirements
Python (3.5, 3.6, 3.7, 3.8, 3.9, 3.10, 3.11)
Django (2.0, 2.1, 2.2, 3.0, 3.1, 3.2, 4.0, 4.1, 4.2)
Django REST Framework (3.8, 3.9, 3.10, 3.11, 3.12, 3.13, 3.14)
Installation
Install using pip
$ pip install drf_serializer_fields_permissions
Example
To show a field only for staff users you need to add show_only_to field to the Meta class of a serializer. show_only_for field must be a dict with two keys - fields and permission_classes. permission_classes is a list or a tuple of permission classes. You can use built-in DRF permission or create your own, but you must override has_permission method of BasePermission class. fields is list or tuple of serializer fields which will be shown only to staff users, in our case.
from rest_framework import serializers
from rest_framework import permissions
from .models import Project
from fields_permissions.mixins import FieldPermissionMixin
class ProjectSerializer(FieldPermissionMixin, serializers.ModelSerializer):
class Meta:
model = Project
fields = ('id', 'name', 'status', 'description', 'team_lead_user')
show_only_for = {
'fields': ('team_lead_user',),
'permission_classes': (permissions.IsAdminUser,)
}
write_only_for = {
'fields': ('status', 'description'),
'permission_classes': (permissions.IsAdminUser,)
}
Testing
Install testing requirements.
$ pip install -r requirements.txt
Run with runtests.
$ ./runtests.py
You can also use the excellent tox testing tool to run the tests against all supported versions of Python and Django. Install tox globally, and then simply run:
$ tox
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
File details
Details for the file drf_serializer_fields_permissions-0.2.0.tar.gz
.
File metadata
- Download URL: drf_serializer_fields_permissions-0.2.0.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 25fc401e90486afa9c76f59f77dc2f441277c732daed32b2f88aadb27ea56e97 |
|
MD5 | cd5209e1251e989f1746e15099065884 |
|
BLAKE2b-256 | 0c8123a0d8dc9bf0d8db792eaed1fe79d087bf49a150711512ccf04c68eb655c |
File details
Details for the file drf_serializer_fields_permissions-0.2.0-py2.py3-none-any.whl
.
File metadata
- Download URL: drf_serializer_fields_permissions-0.2.0-py2.py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 54877a617b80d6fe03872d8cc32c4ece974aecbf7e1a432d70f0752db2b1a3d6 |
|
MD5 | eb3fd3ee8f87df1afd6fe126c8800f23 |
|
BLAKE2b-256 | c4a7a1102c1e5db404603bb43e37d673b263758a1c96089a969099654a6ff341 |