Django REST framework Ext
Some extensions of Django REST framework.
Pagination
DynamicSizePageNumberPagination
Support setting PAGE_QUERY_PARAM (default is page) parameter to specify the page size for querying.
Return all data when the PAGE_QUERY_PARAM (default is limit) parameter is not specified.
Usage:
REST_FRAMEWORK = {
...
'DEFAULT_PAGINATION_CLASS': 'rest_framework_ext.pagination.DynamicSizePageNumberPagination',
...
}
REST_FRAMEWORK_EXT = {
'PAGE_QUERY_PARAM': 'page', # Default is page (If not set)
'PAGE_SIZE_QUERY_PARAM': 'limit',
}
Request:
GET https://api.example.org/accounts/?page=4&limit=100
If no limit is specified, all data will be returned regardless of the page number.
GET https://api.example.org/accounts/
GET https://api.example.org/accounts/?page=4
Permissions
DjangoModelPermissions
Add view permission control.
Usage:
from rest_framework_ext.permissions import DjangoModelPermissions
class MyModelViewSet(viewsets.ModelViewSet):
permission_classes = [DjangoModelPermissions]
Or use globally in settings.py:
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'djangorestframework_ext.permissions.DjangoModelPermissions',
),
}
ReadOnly
Requests will only be permitted if the request method is one of the "safe" methods (GET, HEAD or OPTIONS).
Usage:
from rest_framework_ext.permissions import ReadOnly
IsSuperuser
Determine whether the request user is superuser.
Usage:
from rest_framework_ext.permissions import IsSuperuser
HasPermission
Mainly used for providing permission validation to @api_view.
from rest_framework_ext.permissions import HasPermission
@api_view(['GET'])
@permission_classes([HasPermission('user.change_user')])
def change_user(request):
...
Serializers
ModelSerializer
Using verbose name in error messages.
RecursiveSerializer
Usage:
models.py:
from django.db import models
class Department(models.Model):
name = models.CharField('Name', max_length=100)
parent = models.ForeignKey('self', related_name='children', verbose_name='Parent')
serializers.py:
from rest_framework import serializers
from rest_framework_ext.serializers import RecursiveSerializer
class DepartmentTreeListSerializer(serializers.ModelSerializer):
children = RecursiveSerializer(many=True)
class Meta:
model = Department
fields = '__all__'
Response:
[{
"id": 1,
"children": [{
"id": 2,
"children": [{
"id": 3,
"children": [{
"id": 4,
"children": [],
"name": "aaa",
"parent": 3
}],
"name": "ddd",
"parent": 2
}, {
"id": 5,
"children": [{
"id": 6,
"children": [],
"name": "eee",
"parent": 7
}],
"name": "xxx",
"parent": 2
}],
"name": "yyy",
"parent": 1
}],
"name": "zzz",
"parent": null
}]
DynamicFieldsModelSerializer
It's copied from official document.
Use fields to specify the fields to be used by a serializer at the point of initializing it.
Or use exclude to specify the fields to be excluded by a serializer at the point of initializing it.
Usage:
models.py:
from django.db import models
class Parent(models.Model):
name = models.CharField('name', max_length=100)
class Child(models.Model):
name = models.CharField('name', max_length=100)
parent = models.ForeignKey(Parent, verbose_name='parent')
serializer.py:
from rest_framework import serializers
from rest_framework_ext.serializers import DynamicFieldsModelSerializer
class ChildSerializer(DynamicFieldsModelSerializer):
class Meta:
model = Child
fields = '__all__'
class ParentSerializer(serializers.ModelSerializer):
children = ChildSerializer(many=True, read_only=True, exclude=['parent'])
Mixins
MultipleFieldLookupMixin
From Creating custom mixins and Multiple lookup_fields for django rest framework.
Used for Multiple field lookup.
Usage:
views.py:
from rest_framework import viewsets
from rest_framework_ext.mixins import MultipleFieldLookupMixin
class ExampleViewSet(MultipleFieldLookupMixin, viewsets.ModelViewSet):
lookup_fields = ['pk', 'field_one', 'field_two']
urls.py:
from django.urls import path
from . import views
urlpatterns = [
path(r'examples/<str:field_one>/<str:field_two>/', views.ExampleViewSet.as_view({'get': 'retrieve'}))
]
DisableActionsMixin
Batch disable actions for viewset.
Usage:
from rest_framework import viewsets
from rest_framework_ext.mixins import DisableActionsMixin
class ExampleViewSet(DisableActionsMixin, viewsets.ModelViewSet):
disabled_actions = ['retrieve', 'update', 'custom_action']
Utils
get_default_query_params
Get default query params.
Views
exception_handler
Some exception handlers.
Usage:
REST_FRAMEWORK = {
...
'EXCEPTION_HANDLER': 'rest_framework_ext.views.exception_handler',
...
}
Unify all exception responses into a consistent format:
[
{
"code": "string",
"detail": "string",
"field": "string"
}
]
code and field is optional.
Validators
ActiveValidator
Validate whether the corresponding object is active using the specified key (default is is_active) and value (default is True).
Usage:
from rest_framework import serializers
from rest_framework_ext.validators import ActiveValidator
class MySerializer(serializers.ModelSerializer):
relation = serializers.PrimaryKeyRelatedField(queryset=Relation.objects.all(), validators=[ActiveValidator()])
This requires that the is_active attribute of relation must be set to true.
Metadata
Release files for djangorestframework-ext 0.31
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| djangorestframework_ext-0.31.tar.gz | 7.4 kB | Without provenance | Details for djangorestframework_ext-0.31.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| djangorestframework_ext-0.31-py3-none-any.whl | Python 3 | none | any | Details for djangorestframework_ext-0.31-py3-none-any.whl |
Total release size: 16.7 kB
Release files / djangorestframework_ext-0.31.tar.gz
| Download URL | djangorestframework_ext-0.31.tar.gz |
|---|---|
| Size | 7.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c4498f202de2c943b28e8c42ebc491cf4885e9152869bcfc42c3ea443962057c
|
|
BLAKE2b-256 checksum How to use checksums |
d5c761fdcb88f35e80b826d7d192602b92300eb9d9f86d7a26d128064f2b57cb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.11
|
Release files / djangorestframework_ext-0.31-py3-none-any.whl
| Download URL | djangorestframework_ext-0.31-py3-none-any.whl |
|---|---|
| Size | 9.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b9bf66cf7543e7dba15157c09ca6d25bd3a2f774aa351ffb1abf2a1ed70367ab
|
|
BLAKE2b-256 checksum How to use checksums |
994478f045674e388a69effe1f8aadded7740d38a0c1e5a4e663dcfad6ca846b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.11
|
Security for djangorestframework-ext 0.31
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.