Skip to main content

Dynamically return subset of Django REST Framework serializer fields

Project description

PyPI Downloads Codacy grade License is MIT

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

What It Does

Example serializer:

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

A regular request returns all fields:

GET /identities

[
  {
    "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

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

It also works on single objects!

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

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

Usage

When defining a serializer, use the 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:

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

Credits

  • The implementation is based on this StackOverflow answer. Thanks YAtOff!

  • The GitHub users X17 and rawbeans provided improvements on my gist that were incorporated into this library. Thanks!

License

MIT license, see LICENSE file.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

drf_dynamic_fields-0.1.1.tar.gz (3.0 kB view details)

Uploaded Source

Built Distribution

drf_dynamic_fields-0.1.1-py2.py3-none-any.whl (5.3 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file drf_dynamic_fields-0.1.1.tar.gz.

File metadata

File hashes

Hashes for drf_dynamic_fields-0.1.1.tar.gz
Algorithm Hash digest
SHA256 252e5aa67dee53cffc5b48ce28fd4ba32b3c9cfdf5c953414d8cf85a11ba89ce
MD5 989bc87429bce8274d66e9272ba6ca47
BLAKE2b-256 bfc1f9f70ddc5bfb542ce7e0137586f756d75952cfaca2cfa4f430be2c4ed57a

See more details on using hashes here.

File details

Details for the file drf_dynamic_fields-0.1.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for drf_dynamic_fields-0.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 300d271320ed4504470816064895296dd827aeebfb1c9157ad614557321f1c08
MD5 c7a158672c06e3be770b36de149cd524
BLAKE2b-256 6406e067020dd5b9e346f3b729ada642aa03ce1f6806f00a4ed4a276d25611d7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page