Skip to content

Commit f7e6e2d

Browse files
committed
Initial commit
0 parents  commit f7e6e2d

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.cache/
2+
.coverage
3+
*.swp
4+
*.pyc
5+
__pycache__
6+
dist/
7+
*.egg-info/
8+
build/

LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2014--2016 Danilo Bargen and contributors
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of
4+
this software and associated documentation files (the "Software"), to deal in
5+
the Software without restriction, including without limitation the rights to
6+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7+
of the Software, and to permit persons to whom the Software is furnished to do
8+
so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
SOFTWARE.

README.rst

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
Dynamic Serializer Fields for Django REST Framework
2+
===================================================
3+
4+
This package provides a mixin that allows the user to select only a sbuset of
5+
fields per resource.
6+
7+
Example
8+
-------
9+
10+
Serializer::
11+
12+
class IdentitySerializer(serializers.HyperlinkedModelSerializer):
13+
class Meta:
14+
model = models.Identity
15+
fields = ('id', 'url', 'type', 'data')
16+
17+
A regular request returns all fields:
18+
19+
``GET /identities``
20+
21+
::
22+
23+
[
24+
{
25+
"id": 1,
26+
"url": "http://localhost:8000/api/identities/1/",
27+
"type": 5,
28+
"data": "John Doe"
29+
},
30+
...
31+
]
32+
33+
A query with the `fields` parameter on the other hand returns only a subset of
34+
the fields:
35+
36+
``GET /identities/?fields=id,data``
37+
38+
::
39+
40+
[
41+
{
42+
"id": 1,
43+
"data": "John Doe"
44+
},
45+
...
46+
]
47+
48+
License
49+
-------
50+
51+
MIT license, see ``LICENSE`` file.

drf_dynamic_fields/__init__.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import warnings
2+
3+
4+
class DynamicFieldsMixin(object):
5+
"""
6+
A serializer mixin that takes an additional `fields` argument that controls
7+
which fields should be displayed.
8+
"""
9+
def __init__(self, *args, **kwargs):
10+
super(DynamicFieldsMixin, self).__init__(*args, **kwargs)
11+
12+
# If the context is not set, return
13+
if not self.context:
14+
return
15+
16+
# If the request is not passed in, warn and return
17+
if 'request' not in self.context:
18+
warnings.warn('Context does not have access to request')
19+
return
20+
21+
fields = self.context['request'].query_params.get('fields', None)
22+
if fields:
23+
fields = fields.split(',')
24+
# Drop any fields that are not specified in the `fields` argument.
25+
allowed = set(fields)
26+
existing = set(self.fields.keys())
27+
for field_name in existing - allowed:
28+
self.fields.pop(field_name)

0 commit comments

Comments
 (0)