drf-dynamic-fields


Namedrf-dynamic-fields JSON
Version 0.4.0 PyPI version JSON
download
home_pagehttps://github.com/dbrgn/drf-dynamic-fields
SummaryDynamically return subset of Django REST Framework serializer fields
upload_time2022-04-05 20:48:34
maintainer
docs_urlNone
authorDanilo Bargen
requires_python
licenseMIT
keywords drf restframework rest_framework django_rest_framework serializers
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            Dynamic Serializer Fields for Django REST Framework
===================================================

.. image:: https://secure.travis-ci.org/dbrgn/drf-dynamic-fields.png?branch=master
    :alt: Build status
    :target: http://travis-ci.org/dbrgn/drf-dynamic-fields

.. image:: https://img.shields.io/pypi/v/drf-dynamic-fields.svg
    :alt: PyPI Version
    :target: https://pypi.python.org/pypi/drf-dynamic-fields

.. image:: https://img.shields.io/pypi/dm/drf-dynamic-fields.svg?maxAge=3600
    :alt: PyPI Downloads
    :target: https://pypi.python.org/pypi/drf-dynamic-fields

.. image:: https://img.shields.io/github/license/mashape/apistatus.svg?maxAge=2592000
    :alt: License is MIT
    :target: https://github.com/dbrgn/drf-dynamic-fields/blob/master/LICENSE

This package provides a mixin that allows the user to dynamically select only a
subset of fields per resource.

Official version support:

- Django 2.2 LTS, 3.2 LTS, 4.0
- Supported REST Framework versions: 3.8, 3.9
- Python 3.7+


Scope
-----

This library is about filtering fields based on individual requests. It is
deliberately kept simple and we do not plan to add new features (including
support for nested fields). Feel free to contribute improvements, code
simplifications and bugfixes though! (See also: `#18
<https://github.com/dbrgn/drf-dynamic-fields/issues/18>`__)

If you need more advanced filtering features, maybe `drf-flex-fields
<https://github.com/rsinger86/drf-flex-fields>`_ could be something for you.


Installing
----------

::

    pip install drf-dynamic-fields

What It Does
------------

Example serializer:

.. sourcecode:: python

    class IdentitySerializer(DynamicFieldsMixin, serializers.HyperlinkedModelSerializer):
        class Meta:
            model = models.Identity
            fields = ('id', 'url', 'type', 'data')

A regular request returns all fields:

``GET /identities``

.. sourcecode:: json

    [
      {
        "id": 1,
        "url": "http://localhost:8000/api/identities/1/",
        "type": 5,
        "data": "John Doe"
      },
      ...
    ]

A query with the `fields` parameter on the other hand returns only a subset of
the fields:

``GET /identities/?fields=id,data``

.. sourcecode:: json

    [
      {
        "id": 1,
        "data": "John Doe"
      },
      ...
    ]

And a query with the `omit` parameter excludes specified fields.

``GET /identities/?omit=data``

.. sourcecode:: json

    [
      {
        "id": 1,
        "url": "http://localhost:8000/api/identities/1/",
        "type": 5
      },
      ...
    ]

You can use both `fields` and `omit` in the same request!

``GET /identities/?omit=data,fields=data,id``

.. sourcecode:: json

    [
      {
        "id": 1
      },
      ...
    ]


Though why you would want to do something like that is beyond this author.

It also works on single objects!

``GET /identities/1/?fields=id,data``

.. sourcecode:: json

    {
      "id": 1,
      "data": "John Doe"
    }

Usage
-----

When defining a serializer, use the ``DynamicFieldsMixin``:

.. sourcecode:: python

    from drf_dynamic_fields import DynamicFieldsMixin

    class IdentitySerializer(DynamicFieldsMixin, serializers.ModelSerializer):
        class Meta:
            model = models.Identity
            fields = ('id', 'url', 'type', 'data')

The mixin needs access to the ``request`` object. Some DRF classes like the
``ModelViewSet`` set that by default, but if you handle serializers yourself,
pass in the request through the context:

.. sourcecode:: python

    events = Event.objects.all()
    serializer = EventSerializer(events, many=True, context={'request': request})


Warnings
--------

If the request context does not have access to the request, a warning is
emitted::

   UserWarning: Context does not have access to request.

First, make sure that you are passing the request to the serializer context (see
"Usage" section).

There are some cases (e.g. nested serializers) where you cannot get rid of the
warning that way (see `issue 27 <https://github.com/dbrgn/drf-dynamic-fields/issues/27>`_).
In that case, you can silence the warning through ``settings.py``:

.. sourcecode:: python

   DRF_DYNAMIC_FIELDS = {
      'SUPPRESS_CONTEXT_WARNING': True,
   }


Testing
-------

To run tests, install Django and DRF and then run ``runtests.py``:

    $ python runtests.py


Credits
-------

- The implementation is based on `this
  <http://stackoverflow.com/a/23674297/284318>`__ StackOverflow answer. Thanks
  ``YAtOff``!
- The GitHub users ``X17`` and ``rawbeans`` provided improvements on `my gist
  <https://gist.github.com/dbrgn/4e6fc1fe5922598592d6>`__ that were incorporated
  into this library. Thanks!
- For other contributors, please see `Github contributor stats
  <https://github.com/dbrgn/drf-dynamic-fields/graphs/contributors>`__.


License
-------

MIT license, see ``LICENSE`` file.



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/dbrgn/drf-dynamic-fields",
    "name": "drf-dynamic-fields",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "drf restframework rest_framework django_rest_framework serializers",
    "author": "Danilo Bargen",
    "author_email": "mail@dbrgn.ch",
    "download_url": "https://files.pythonhosted.org/packages/d2/7f/1c17e0791b8d028b47908be78ac8041c544bd07a59afd8556304a0f0d355/drf_dynamic_fields-0.4.0.tar.gz",
    "platform": null,
    "description": "Dynamic Serializer Fields for Django REST Framework\n===================================================\n\n.. image:: https://secure.travis-ci.org/dbrgn/drf-dynamic-fields.png?branch=master\n    :alt: Build status\n    :target: http://travis-ci.org/dbrgn/drf-dynamic-fields\n\n.. image:: https://img.shields.io/pypi/v/drf-dynamic-fields.svg\n    :alt: PyPI Version\n    :target: https://pypi.python.org/pypi/drf-dynamic-fields\n\n.. image:: https://img.shields.io/pypi/dm/drf-dynamic-fields.svg?maxAge=3600\n    :alt: PyPI Downloads\n    :target: https://pypi.python.org/pypi/drf-dynamic-fields\n\n.. image:: https://img.shields.io/github/license/mashape/apistatus.svg?maxAge=2592000\n    :alt: License is MIT\n    :target: https://github.com/dbrgn/drf-dynamic-fields/blob/master/LICENSE\n\nThis package provides a mixin that allows the user to dynamically select only a\nsubset of fields per resource.\n\nOfficial version support:\n\n- Django 2.2 LTS, 3.2 LTS, 4.0\n- Supported REST Framework versions: 3.8, 3.9\n- Python 3.7+\n\n\nScope\n-----\n\nThis library is about filtering fields based on individual requests. It is\ndeliberately kept simple and we do not plan to add new features (including\nsupport for nested fields). Feel free to contribute improvements, code\nsimplifications and bugfixes though! (See also: `#18\n<https://github.com/dbrgn/drf-dynamic-fields/issues/18>`__)\n\nIf you need more advanced filtering features, maybe `drf-flex-fields\n<https://github.com/rsinger86/drf-flex-fields>`_ could be something for you.\n\n\nInstalling\n----------\n\n::\n\n    pip install drf-dynamic-fields\n\nWhat It Does\n------------\n\nExample serializer:\n\n.. sourcecode:: python\n\n    class IdentitySerializer(DynamicFieldsMixin, serializers.HyperlinkedModelSerializer):\n        class Meta:\n            model = models.Identity\n            fields = ('id', 'url', 'type', 'data')\n\nA regular request returns all fields:\n\n``GET /identities``\n\n.. sourcecode:: json\n\n    [\n      {\n        \"id\": 1,\n        \"url\": \"http://localhost:8000/api/identities/1/\",\n        \"type\": 5,\n        \"data\": \"John Doe\"\n      },\n      ...\n    ]\n\nA query with the `fields` parameter on the other hand returns only a subset of\nthe fields:\n\n``GET /identities/?fields=id,data``\n\n.. sourcecode:: json\n\n    [\n      {\n        \"id\": 1,\n        \"data\": \"John Doe\"\n      },\n      ...\n    ]\n\nAnd a query with the `omit` parameter excludes specified fields.\n\n``GET /identities/?omit=data``\n\n.. sourcecode:: json\n\n    [\n      {\n        \"id\": 1,\n        \"url\": \"http://localhost:8000/api/identities/1/\",\n        \"type\": 5\n      },\n      ...\n    ]\n\nYou can use both `fields` and `omit` in the same request!\n\n``GET /identities/?omit=data,fields=data,id``\n\n.. sourcecode:: json\n\n    [\n      {\n        \"id\": 1\n      },\n      ...\n    ]\n\n\nThough why you would want to do something like that is beyond this author.\n\nIt also works on single objects!\n\n``GET /identities/1/?fields=id,data``\n\n.. sourcecode:: json\n\n    {\n      \"id\": 1,\n      \"data\": \"John Doe\"\n    }\n\nUsage\n-----\n\nWhen defining a serializer, use the ``DynamicFieldsMixin``:\n\n.. sourcecode:: python\n\n    from drf_dynamic_fields import DynamicFieldsMixin\n\n    class IdentitySerializer(DynamicFieldsMixin, serializers.ModelSerializer):\n        class Meta:\n            model = models.Identity\n            fields = ('id', 'url', 'type', 'data')\n\nThe mixin needs access to the ``request`` object. Some DRF classes like the\n``ModelViewSet`` set that by default, but if you handle serializers yourself,\npass in the request through the context:\n\n.. sourcecode:: python\n\n    events = Event.objects.all()\n    serializer = EventSerializer(events, many=True, context={'request': request})\n\n\nWarnings\n--------\n\nIf the request context does not have access to the request, a warning is\nemitted::\n\n   UserWarning: Context does not have access to request.\n\nFirst, make sure that you are passing the request to the serializer context (see\n\"Usage\" section).\n\nThere are some cases (e.g. nested serializers) where you cannot get rid of the\nwarning that way (see `issue 27 <https://github.com/dbrgn/drf-dynamic-fields/issues/27>`_).\nIn that case, you can silence the warning through ``settings.py``:\n\n.. sourcecode:: python\n\n   DRF_DYNAMIC_FIELDS = {\n      'SUPPRESS_CONTEXT_WARNING': True,\n   }\n\n\nTesting\n-------\n\nTo run tests, install Django and DRF and then run ``runtests.py``:\n\n    $ python runtests.py\n\n\nCredits\n-------\n\n- The implementation is based on `this\n  <http://stackoverflow.com/a/23674297/284318>`__ StackOverflow answer. Thanks\n  ``YAtOff``!\n- The GitHub users ``X17`` and ``rawbeans`` provided improvements on `my gist\n  <https://gist.github.com/dbrgn/4e6fc1fe5922598592d6>`__ that were incorporated\n  into this library. Thanks!\n- For other contributors, please see `Github contributor stats\n  <https://github.com/dbrgn/drf-dynamic-fields/graphs/contributors>`__.\n\n\nLicense\n-------\n\nMIT license, see ``LICENSE`` file.\n\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Dynamically return subset of Django REST Framework serializer fields",
    "version": "0.4.0",
    "split_keywords": [
        "drf",
        "restframework",
        "rest_framework",
        "django_rest_framework",
        "serializers"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "1ebacf7a9021d6539c1f3201cf887d03",
                "sha256": "48b879fe899905bc18593a61bca43e3b595dc3431b3b4ee499a9fd6c9a53f98c"
            },
            "downloads": -1,
            "filename": "drf_dynamic_fields-0.4.0-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "1ebacf7a9021d6539c1f3201cf887d03",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": null,
            "size": 5451,
            "upload_time": "2022-04-05T20:48:32",
            "upload_time_iso_8601": "2022-04-05T20:48:32.452720Z",
            "url": "https://files.pythonhosted.org/packages/2d/18/4b02bda9ae5a7ac52bfcb4f1740fb4a0aa12d220881695471b700bb3f00e/drf_dynamic_fields-0.4.0-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "6ed20f52517a140d66af9755b2f13636",
                "sha256": "f20a5ec27d003db7595c9315db22217493dcaed575f3811d3e12f264c791c20c"
            },
            "downloads": -1,
            "filename": "drf_dynamic_fields-0.4.0.tar.gz",
            "has_sig": false,
            "md5_digest": "6ed20f52517a140d66af9755b2f13636",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 4864,
            "upload_time": "2022-04-05T20:48:34",
            "upload_time_iso_8601": "2022-04-05T20:48:34.091103Z",
            "url": "https://files.pythonhosted.org/packages/d2/7f/1c17e0791b8d028b47908be78ac8041c544bd07a59afd8556304a0f0d355/drf_dynamic_fields-0.4.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-04-05 20:48:34",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "dbrgn",
    "github_project": "drf-dynamic-fields",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "drf-dynamic-fields"
}
        
Elapsed time: 0.02049s