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': 'djangorestframework_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
```
# Permissions
## DjangoModelPermissions
Add ``view`` permission control.
Usage:
```
from djangorestframework_ext.permissions import DjangoModelPermissions
```
## ReadOnly
Requests will only be permitted if the request method is one of the "safe" methods (`GET`, `HEAD` or `OPTIONS`).
Usage:
```
from djangorestframework_ext.permissions import ReadOnly
```
## IsCurrentUser
Determine whether the object is the current login user.
Usage:
```
from djangorestframework_ext.permissions import IsCurrentUser
```
## IsSuperuser
Determine whether the request user is superuser.
Usage:
```
from djangorestframework_ext.permissions import IsSuperuser
```
## HasPermission
Mainly used for providing permission validation to `@api_view`.
```
@api_view(['GET'])
@permission_classes([HasPermission('user.change_user')])
def change_user(request):
...
```
# Serializers
## RecursiveSerializer
Usage:
```
from rest_framework import serializers
from djangorestframework_ext.serializers import RecursiveSerializer
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')
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
}]
```
## ExportModelSerializer
Use verbose name or label replace field name.
Usage:
```
from djangorestframework_ext.serializers import ExportModelSerializer
from django.db import models
class Department(models.Model):
name = models.CharField('Name', max_length=100)
creator = models.ForeignKey(User, null=False, verbose_name='Creator')
class DepartmentExportSerializer(ExportModelSerializer):
creator = serializers.StringRelatedField(label='Creator', read_only=True)
class Meta:
model = Department
fields = ['name', 'creator']
```
Response:
```
[{
"Name": "aaa",
"Creator": "John"
}]
```
## DynamicFieldsModelSerializer
It's copied from [official document](https://www.django-rest-framework.org/api-guide/serializers/#dynamically-modifying-fields).
# Mixins
## MultiFieldLookupMixin
From [Multiple lookup_fields for django rest framework](https://stackoverflow.com/questions/38461366/multiple-lookup-fields-for-django-rest-framework).
Used for multi field lookup.
Usage:
views.py:
```
class ExampleViewSet(MultipleFieldLookupMixin, viewsets.ModelViewSet):
lookup_fields = ['pk', 'field_one', 'field_two']
```
urls.py:
```
urlpatterns = [
path(r'examples/<str:field_one>/<str:field_two>/', views.ExampleViewSet.as_view({'get': 'retrieve'}))
]
```
# ViewSets
## ExportModelViewSet
Use `epxort` action and `ExportPermission` to exporting data.
# Utils
## get_default_query_params
Get default query params.
# Views
## exception_handler
Some exception handlers.
Usage:
```
REST_FRAMEWORK = {
...
'EXCEPTION_HANDLER': 'djangorestframework_ext.views.exception_handler',
...
}
```
# 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
class MySerializer(serializers.ModelSerializer):
relation = serializers.PrimaryKeyRelatedField(queryset=Relation.objects.all(), validators=[ActiveValidator()])
```
Raw data
{
"_id": null,
"home_page": "https://github.com/zengqiu/django-rest-framework-ext",
"name": "djangorestframework-ext",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.5",
"maintainer_email": null,
"keywords": null,
"author": "zengqiu",
"author_email": "zengqiu@qq.com",
"download_url": "https://files.pythonhosted.org/packages/6d/de/a17ba5b2f7df08be8a17594e7e5397ce53180df4078e4e1c4a75325be0fa/djangorestframework_ext-0.26.tar.gz",
"platform": "any",
"description": "Django REST framework Ext\r\n=========================\r\n\r\nSome extensions of Django REST framework.\r\n\r\n# Pagination\r\n\r\n## DynamicSizePageNumberPagination\r\n\r\nSupport setting `PAGE_QUERY_PARAM` (default is `page`) parameter to specify the page size for querying.\r\n\r\nReturn all data when the `PAGE_QUERY_PARAM` (default is `limit`) parameter is not specified.\r\n\r\nUsage:\r\n\r\n```\r\nREST_FRAMEWORK = {\r\n ...\r\n 'DEFAULT_PAGINATION_CLASS': 'djangorestframework_ext.pagination.DynamicSizePageNumberPagination',\r\n ...\r\n}\r\n\r\nREST_FRAMEWORK_EXT = {\r\n 'PAGE_QUERY_PARAM': 'page', # Default is page (If not set)\r\n 'PAGE_SIZE_QUERY_PARAM': 'limit',\r\n}\r\n```\r\n\r\nRequest:\r\n\r\n```\r\nGET https://api.example.org/accounts/?page=4&limit=100\r\n```\r\n\r\n# Permissions\r\n\r\n## DjangoModelPermissions\r\n\r\nAdd ``view`` permission control.\r\n\r\nUsage:\r\n\r\n```\r\nfrom djangorestframework_ext.permissions import DjangoModelPermissions\r\n```\r\n\r\n## ReadOnly\r\n\r\nRequests will only be permitted if the request method is one of the \"safe\" methods (`GET`, `HEAD` or `OPTIONS`).\r\n\r\nUsage:\r\n\r\n```\r\nfrom djangorestframework_ext.permissions import ReadOnly\r\n```\r\n\r\n## IsCurrentUser\r\n\r\nDetermine whether the object is the current login user.\r\n\r\nUsage:\r\n\r\n```\r\nfrom djangorestframework_ext.permissions import IsCurrentUser\r\n```\r\n\r\n## IsSuperuser\r\n\r\nDetermine whether the request user is superuser.\r\n\r\nUsage:\r\n\r\n```\r\nfrom djangorestframework_ext.permissions import IsSuperuser\r\n```\r\n\r\n## HasPermission\r\n\r\nMainly used for providing permission validation to `@api_view`.\r\n\r\n```\r\n@api_view(['GET'])\r\n@permission_classes([HasPermission('user.change_user')])\r\ndef change_user(request):\r\n ...\r\n```\r\n\r\n# Serializers\r\n\r\n## RecursiveSerializer\r\n\r\nUsage:\r\n\r\n```\r\nfrom rest_framework import serializers\r\nfrom djangorestframework_ext.serializers import RecursiveSerializer\r\nfrom django.db import models\r\n\r\n\r\nclass Department(models.Model):\r\n name = models.CharField('Name', max_length=100)\r\n parent = models.ForeignKey('self', related_name='children', verbose_name='Parent')\r\n\r\n\r\nclass DepartmentTreeListSerializer(serializers.ModelSerializer):\r\n children = RecursiveSerializer(many=True)\r\n\r\n class Meta:\r\n model = Department\r\n fields = '__all__'\r\n```\r\n\r\nResponse:\r\n\r\n```\r\n[{\r\n \"id\": 1,\r\n \"children\": [{\r\n \"id\": 2,\r\n \"children\": [{\r\n \"id\": 3,\r\n \"children\": [{\r\n \"id\": 4,\r\n \"children\": [],\r\n \"name\": \"aaa\",\r\n \"parent\": 3\r\n }],\r\n \"name\": \"ddd\",\r\n \"parent\": 2\r\n }, {\r\n \"id\": 5,\r\n \"children\": [{\r\n \"id\": 6,\r\n \"children\": [],\r\n \"name\": \"eee\",\r\n \"parent\": 7\r\n }],\r\n \"name\": \"xxx\",\r\n \"parent\": 2\r\n }],\r\n \"name\": \"yyy\",\r\n \"parent\": 1\r\n }],\r\n \"name\": \"zzz\",\r\n \"parent\": null\r\n}]\r\n```\r\n\r\n## ExportModelSerializer\r\n\r\nUse verbose name or label replace field name.\r\n\r\nUsage:\r\n\r\n```\r\nfrom djangorestframework_ext.serializers import ExportModelSerializer\r\nfrom django.db import models\r\n\r\n\r\nclass Department(models.Model):\r\n name = models.CharField('Name', max_length=100)\r\n creator = models.ForeignKey(User, null=False, verbose_name='Creator')\r\n\r\n\r\nclass DepartmentExportSerializer(ExportModelSerializer):\r\n creator = serializers.StringRelatedField(label='Creator', read_only=True)\r\n \r\n class Meta:\r\n model = Department\r\n fields = ['name', 'creator']\r\n```\r\n\r\nResponse:\r\n\r\n```\r\n[{\r\n \"Name\": \"aaa\",\r\n \"Creator\": \"John\"\r\n}]\r\n```\r\n\r\n## DynamicFieldsModelSerializer\r\n\r\nIt's copied from [official document](https://www.django-rest-framework.org/api-guide/serializers/#dynamically-modifying-fields).\r\n\r\n# Mixins\r\n\r\n## MultiFieldLookupMixin\r\n\r\nFrom [Multiple lookup_fields for django rest framework](https://stackoverflow.com/questions/38461366/multiple-lookup-fields-for-django-rest-framework).\r\n\r\nUsed for multi field lookup.\r\n\r\nUsage:\r\n\r\nviews.py:\r\n\r\n```\r\nclass ExampleViewSet(MultipleFieldLookupMixin, viewsets.ModelViewSet):\r\n lookup_fields = ['pk', 'field_one', 'field_two']\r\n```\r\n\r\nurls.py:\r\n\r\n```\r\nurlpatterns = [\r\n path(r'examples/<str:field_one>/<str:field_two>/', views.ExampleViewSet.as_view({'get': 'retrieve'}))\r\n]\r\n```\r\n\r\n# ViewSets\r\n\r\n## ExportModelViewSet\r\n\r\nUse `epxort` action and `ExportPermission` to exporting data.\r\n\r\n# Utils\r\n\r\n## get_default_query_params\r\n\r\nGet default query params.\r\n\r\n# Views\r\n\r\n## exception_handler\r\n\r\nSome exception handlers.\r\n\r\nUsage: \r\n\r\n```\r\nREST_FRAMEWORK = {\r\n ...\r\n 'EXCEPTION_HANDLER': 'djangorestframework_ext.views.exception_handler',\r\n ...\r\n}\r\n```\r\n\r\n# Validators\r\n\r\n## ActiveValidator\r\n\r\nValidate whether the corresponding object is active using the specified key (default is `is_active`) and value (default is `True`).\r\n\r\nUsage:\r\n\r\n```\r\nfrom rest_framework import serializers\r\n\r\nclass MySerializer(serializers.ModelSerializer):\r\n relation = serializers.PrimaryKeyRelatedField(queryset=Relation.objects.all(), validators=[ActiveValidator()])\r\n```\r\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Django Rest framework Extensions",
"version": "0.26",
"project_urls": {
"Homepage": "https://github.com/zengqiu/django-rest-framework-ext"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "366fbfdb63a6ef62ed96bda25a279be26917394539a15899dbe8d7c5904f1a66",
"md5": "8517d03020421b3dabf8f2feb3c5271c",
"sha256": "4a0abc23152149073b9c011b06a8c733eed8d613606c376ca1c275667b43e177"
},
"downloads": -1,
"filename": "djangorestframework_ext-0.26-py3-none-any.whl",
"has_sig": false,
"md5_digest": "8517d03020421b3dabf8f2feb3c5271c",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.5",
"size": 9027,
"upload_time": "2024-05-09T06:59:21",
"upload_time_iso_8601": "2024-05-09T06:59:21.147816Z",
"url": "https://files.pythonhosted.org/packages/36/6f/bfdb63a6ef62ed96bda25a279be26917394539a15899dbe8d7c5904f1a66/djangorestframework_ext-0.26-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "6ddea17ba5b2f7df08be8a17594e7e5397ce53180df4078e4e1c4a75325be0fa",
"md5": "ac8618b35f121ed826a637ba87e968c6",
"sha256": "901c938b2c589026d8aa445a5f74b067b86a3fe5862e801a782a87a62582a132"
},
"downloads": -1,
"filename": "djangorestframework_ext-0.26.tar.gz",
"has_sig": false,
"md5_digest": "ac8618b35f121ed826a637ba87e968c6",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.5",
"size": 6932,
"upload_time": "2024-05-09T06:59:22",
"upload_time_iso_8601": "2024-05-09T06:59:22.983464Z",
"url": "https://files.pythonhosted.org/packages/6d/de/a17ba5b2f7df08be8a17594e7e5397ce53180df4078e4e1c4a75325be0fa/djangorestframework_ext-0.26.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-05-09 06:59:22",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "zengqiu",
"github_project": "django-rest-framework-ext",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "djangorestframework-ext"
}