drfexts


Namedrfexts JSON
Version 3.12.4 PyPI version JSON
download
home_pagehttps://github.com/aiden520/drfexts
SummaryDjango Restframework Utils
upload_time2024-11-16 09:44:55
maintainerNone
docs_urlNone
authoraiden
requires_python<4.0.0,>=3.10
licenseApache-2.0
keywords django restframework
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            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/02/6b/7da2476cf2ac91f90961fe328de9ca5dde184c39b4a116cce0c097530dd4/drfexts-3.12.4.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.12.4",
    "project_urls": {
        "Homepage": "https://github.com/aiden520/drfexts",
        "Repository": "https://github.com/aiden520/drfexts"
    },
    "split_keywords": [
        "django",
        " restframework"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e9678cd270ba98704dd8251fd658828674a1d864ac1641368f08104da678dd7d",
                "md5": "821a7adac3f7a937317391b205295036",
                "sha256": "e1c79a0ac80006a94830aa3d13870e5bce3403e223ac5b5a2f194446a6417453"
            },
            "downloads": -1,
            "filename": "drfexts-3.12.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "821a7adac3f7a937317391b205295036",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0.0,>=3.10",
            "size": 49733,
            "upload_time": "2024-11-16T09:44:52",
            "upload_time_iso_8601": "2024-11-16T09:44:52.704867Z",
            "url": "https://files.pythonhosted.org/packages/e9/67/8cd270ba98704dd8251fd658828674a1d864ac1641368f08104da678dd7d/drfexts-3.12.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "026b7da2476cf2ac91f90961fe328de9ca5dde184c39b4a116cce0c097530dd4",
                "md5": "4c53ce09944468095540ea7227c971f7",
                "sha256": "e37f8cc92568d494cba8a9d7639ae43588f921a302a18a840c635f112a92547a"
            },
            "downloads": -1,
            "filename": "drfexts-3.12.4.tar.gz",
            "has_sig": false,
            "md5_digest": "4c53ce09944468095540ea7227c971f7",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0.0,>=3.10",
            "size": 38494,
            "upload_time": "2024-11-16T09:44:55",
            "upload_time_iso_8601": "2024-11-16T09:44:55.906050Z",
            "url": "https://files.pythonhosted.org/packages/02/6b/7da2476cf2ac91f90961fe328de9ca5dde184c39b4a116cce0c097530dd4/drfexts-3.12.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-16 09:44:55",
    "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"
}
        
Elapsed time: 0.88009s