Dynamically return subset of Django REST Framework serializer fields
Project description
This package provides a mixin that allows the user to dynamically select only a subset of fields per resource.
Installing
pip install drf-dynamic-fields
What It Does
Example serializer:
class IdentitySerializer(DynamicFieldsMixin, 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"
},
...
]
And a query with the omit parameter excludes specified fields.
GET /identities/?omit=data
[
{
"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
[
{
"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
{
"id": 1,
"data": "John Doe"
}
Usage
When defining a serializer, use the DynamicFieldsMixin:
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:
events = Event.objects.all()
serializer = EventSerializer(events, many=True, context={'request': request})
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. Feel free to contribute improvements, code simplifications and bugfixes though! (See also: #18)
If you need more advanced filtering features, maybe drf-flex-fields could be something for you.
Testing
To run tests, install Django and DRF and then run runtests.py:
$ python runtests.py
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!
For other contributors, please see Github contributor stats.
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
Built Distribution
File details
Details for the file drf_dynamic_fields-0.3.0.tar.gz
.
File metadata
- Download URL: drf_dynamic_fields-0.3.0.tar.gz
- Upload date:
- Size: 4.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 245eed41664976451b51ab9b59af179692ab6d8ee260a6b85ac2d44d2b06f5b4 |
|
MD5 | 2b96c5eadf469e85b0e3651a0aca91dc |
|
BLAKE2b-256 | ea9347a00626ee1b6764932cc8d3fea318cb17fc1554f30fd4b13c84291aed7d |
File details
Details for the file drf_dynamic_fields-0.3.0-py2.py3-none-any.whl
.
File metadata
- Download URL: drf_dynamic_fields-0.3.0-py2.py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5da7d856f411561cb25d6ce3a0a152c2bd441d41a81b279ca0fec214d14c6134 |
|
MD5 | 9c939228d73f218214720791d2a2700b |
|
BLAKE2b-256 | 5b1e53b0534ea981eb62b4e0cc695e589586b4d111cc0e2e4bbad498f32d2b3f |