Created
June 3, 2017 11:24
-
-
Save curiousest/3b5158eba0e0b5524dd2ff15d60dd14f to your computer and use it in GitHub Desktop.
DRF reduce serializer fields mixin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ReduceFieldsMixin: | |
| def get_fields(self, *args, **kwargs): | |
| fields = super().get_fields(*args, **kwargs) | |
| query_params = getattr(self.context.get('request', {}), 'query_params', {}) | |
| requested_fields = query_params.get('fields', None) | |
| if requested_fields: | |
| requested_fields = requested_fields.split(',') | |
| fields = {key: value for key, value in fields.items() if key in requested_fields} | |
| return fields |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment