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.
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"
},
...
]
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:
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})
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.2.0.tar.gz
.
File metadata
- Download URL: drf_dynamic_fields-0.2.0.tar.gz
- Upload date:
- Size: 3.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6c4401b4f939204974ee35b1c9f51f0e0afde6c63f6a6874bfb1d981ccb728e2 |
|
MD5 | e841924c9df3eec1e3c8f7ea3d5d4796 |
|
BLAKE2b-256 | 628ec05f2f4671ab634670cf70dc9a9e0ddbb4e27964d9892581c722b7230b97 |
File details
Details for the file drf_dynamic_fields-0.2.0-py2.py3-none-any.whl
.
File metadata
- Download URL: drf_dynamic_fields-0.2.0-py2.py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 90aa50b03b0bb6ee27d37fdb8794e8c849a5f6a900aa41a39b03c873049c2029 |
|
MD5 | 97bbbd800983d02983054d6f90526a9c |
|
BLAKE2b-256 | b486492366db2071fb8def7a51f974cfc154c02d8323939b42005262ea7294c9 |