drfexts
=======
[![GitHub license](https://img.shields.io/github/license/aiden520/drfexts)](https://github.com/aiden520/drfexts/blob/master/LICENSE)
[![pypi-version](https://img.shields.io/pypi/v/drfexts.svg)](https://pypi.python.org/pypi/drfexts)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/drfexts)
[![PyPI - Django Version](https://img.shields.io/badge/django-%3E%3D3.0-44B78B)](https://www.djangoproject.com/)
[![PyPI - DRF Version](https://img.shields.io/badge/djangorestframework-%3E%3D3.0-red)](https://www.django-rest-framework.org)
[![Build Status](https://app.travis-ci.com/aiden520/drfexts.svg?branch=master)](https://app.travis-ci.com/aiden520/drfexts)
[![Black code style](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/ambv/black)
**Extensions for Django REST Framework**
Installation
------------
``` {.bash}
$ pip install drfexts
```
Usage
-----
*views.py*
``` {.python}
from rest_framework.views import APIView
from rest_framework.settings import api_settings
from drfexts.viewsets import ExportMixin
class MyView (ExportMixin, APIView):
...
```
Ordered Fields
--------------
By default, a `CSVRenderer` will output fields in sorted order. To
specify an alternative field ordering you can override the `header`
attribute. There are two ways to do this:
1) Create a new renderer class and override the `header` attribute
directly:
> ``` {.python}
> class MyUserRenderer (CSVRenderer):
> header = ['first', 'last', 'email']
>
> @api_view(['GET'])
> @renderer_classes((MyUserRenderer,))
> def my_view(request):
> users = User.objects.filter(active=True)
> content = [{'first': user.first_name,
> 'last': user.last_name,
> 'email': user.email}
> for user in users]
> return Response(content)
> ```
2) Use the `renderer_context` to override the field ordering on the
fly:
> ``` {.python}
> class MyView (APIView):
> renderer_classes = [CSVRenderer]
>
> def get_renderer_context(self):
> context = super().get_renderer_context()
> context['header'] = (
> self.request.GET['fields'].split(',')
> if 'fields' in self.request.GET else None)
> return context
>
> ...
> ```
Labeled Fields
--------------
Custom labels can be applied to the `CSVRenderer` using the `labels`
dict attribute where each key corresponds to the header and the value
corresponds to the custom label for that header.
1\) Create a new renderer class and override the `header` and `labels`
attribute directly:
> ``` {.python}
> class MyBazRenderer (CSVRenderer):
> header = ['foo.bar']
> labels = {
> 'foo.bar': 'baz'
> }
> ```
Pagination
----------
Using the renderer with paginated data is also possible with the new
[PaginatedCSVRenderer]{.title-ref} class and should be used with views
that paginate data
For more information about using renderers with Django REST Framework,
see the [API
Guide](http://django-rest-framework.org/api-guide/renderers/) or the
[Tutorial](http://django-rest-framework.org/tutorial/1-serialization/).
Running the tests
-----------------
To run the tests against the current environment:
``` {.bash}
$ ./manage.py test
```
### Changelog
1.0.0
-----
- Initial release
## Thanks
[![PyCharm](docs/pycharm.svg)](https://www.jetbrains.com/?from=drfexts)
Raw data
{
"_id": null,
"home_page": "https://github.com/aiden520/drfexts",
"name": "drfexts",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0.0,>=3.10",
"maintainer_email": null,
"keywords": "django, restframework",
"author": "aiden",
"author_email": "allaher@icloud.com",
"download_url": "https://files.pythonhosted.org/packages/0d/b5/9ce0282cd34d0a8c5e629c674a3623c8e76435944d0cd79ba3ec07dd892f/drfexts-3.13.1.tar.gz",
"platform": null,
"description": "drfexts\n=======\n\n[![GitHub license](https://img.shields.io/github/license/aiden520/drfexts)](https://github.com/aiden520/drfexts/blob/master/LICENSE)\n[![pypi-version](https://img.shields.io/pypi/v/drfexts.svg)](https://pypi.python.org/pypi/drfexts)\n![PyPI - Python Version](https://img.shields.io/pypi/pyversions/drfexts)\n[![PyPI - Django Version](https://img.shields.io/badge/django-%3E%3D3.0-44B78B)](https://www.djangoproject.com/)\n[![PyPI - DRF Version](https://img.shields.io/badge/djangorestframework-%3E%3D3.0-red)](https://www.django-rest-framework.org)\n[![Build Status](https://app.travis-ci.com/aiden520/drfexts.svg?branch=master)](https://app.travis-ci.com/aiden520/drfexts)\n[![Black code style](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/ambv/black)\n\n\n**Extensions for Django REST Framework**\n\n\nInstallation\n------------\n\n``` {.bash}\n$ pip install drfexts\n```\n\nUsage\n-----\n\n*views.py*\n\n``` {.python}\nfrom rest_framework.views import APIView\nfrom rest_framework.settings import api_settings\nfrom drfexts.viewsets import ExportMixin\n\nclass MyView (ExportMixin, APIView):\n ...\n```\n\nOrdered Fields\n--------------\n\nBy default, a `CSVRenderer` will output fields in sorted order. To\nspecify an alternative field ordering you can override the `header`\nattribute. There are two ways to do this:\n\n1) Create a new renderer class and override the `header` attribute\n directly:\n\n > ``` {.python}\n > class MyUserRenderer (CSVRenderer):\n > header = ['first', 'last', 'email']\n >\n > @api_view(['GET'])\n > @renderer_classes((MyUserRenderer,))\n > def my_view(request):\n > users = User.objects.filter(active=True)\n > content = [{'first': user.first_name,\n > 'last': user.last_name,\n > 'email': user.email}\n > for user in users]\n > return Response(content)\n > ```\n\n2) Use the `renderer_context` to override the field ordering on the\n fly:\n\n > ``` {.python}\n > class MyView (APIView):\n > renderer_classes = [CSVRenderer]\n >\n > def get_renderer_context(self):\n > context = super().get_renderer_context()\n > context['header'] = (\n > self.request.GET['fields'].split(',')\n > if 'fields' in self.request.GET else None)\n > return context\n >\n > ...\n > ```\n\nLabeled Fields\n--------------\n\nCustom labels can be applied to the `CSVRenderer` using the `labels`\ndict attribute where each key corresponds to the header and the value\ncorresponds to the custom label for that header.\n\n1\\) Create a new renderer class and override the `header` and `labels`\nattribute directly:\n\n> ``` {.python}\n> class MyBazRenderer (CSVRenderer):\n> header = ['foo.bar']\n> labels = {\n> 'foo.bar': 'baz'\n> }\n> ```\n\n\nPagination\n----------\n\nUsing the renderer with paginated data is also possible with the new\n[PaginatedCSVRenderer]{.title-ref} class and should be used with views\nthat paginate data\n\nFor more information about using renderers with Django REST Framework,\nsee the [API\nGuide](http://django-rest-framework.org/api-guide/renderers/) or the\n[Tutorial](http://django-rest-framework.org/tutorial/1-serialization/).\n\n\nRunning the tests\n-----------------\n\nTo run the tests against the current environment:\n\n``` {.bash}\n$ ./manage.py test\n```\n\n### Changelog\n\n1.0.0\n-----\n\n\n- Initial release\n\n## Thanks\n\n[![PyCharm](docs/pycharm.svg)](https://www.jetbrains.com/?from=drfexts)\n",
"bugtrack_url": null,
"license": "Apache-2.0",
"summary": "Django Restframework Utils",
"version": "3.13.1",
"project_urls": {
"Homepage": "https://github.com/aiden520/drfexts",
"Repository": "https://github.com/aiden520/drfexts"
},
"split_keywords": [
"django",
" restframework"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "ed7be5d84389f22c118b4a507796275c37ff7020d4a8b83de741388bfb9f3313",
"md5": "ed6edf2d2bc32d2e1c23f85e497c218e",
"sha256": "786f224841e18f11db820575f73839fb6333399b603d11013b78442d1b3ae878"
},
"downloads": -1,
"filename": "drfexts-3.13.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "ed6edf2d2bc32d2e1c23f85e497c218e",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0.0,>=3.10",
"size": 49887,
"upload_time": "2024-12-18T06:42:58",
"upload_time_iso_8601": "2024-12-18T06:42:58.042911Z",
"url": "https://files.pythonhosted.org/packages/ed/7b/e5d84389f22c118b4a507796275c37ff7020d4a8b83de741388bfb9f3313/drfexts-3.13.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "0db59ce0282cd34d0a8c5e629c674a3623c8e76435944d0cd79ba3ec07dd892f",
"md5": "5312c7e371dab1ddeb71fde3ce8c0c99",
"sha256": "35c31bf74805ec035f1c14e980276c6bded63aecf1f5e9fee9023a6b56294304"
},
"downloads": -1,
"filename": "drfexts-3.13.1.tar.gz",
"has_sig": false,
"md5_digest": "5312c7e371dab1ddeb71fde3ce8c0c99",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0.0,>=3.10",
"size": 38725,
"upload_time": "2024-12-18T06:42:59",
"upload_time_iso_8601": "2024-12-18T06:42:59.939500Z",
"url": "https://files.pythonhosted.org/packages/0d/b5/9ce0282cd34d0a8c5e629c674a3623c8e76435944d0cd79ba3ec07dd892f/drfexts-3.13.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-12-18 06:42:59",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "aiden520",
"github_project": "drfexts",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "drfexts"
}