Created
July 9, 2019 11:15
-
-
Save akshar-raaj/974e770b0e18145d1573fa59e32647b1 to your computer and use it in GitHub Desktop.
DRF Vanilla view serializer
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
| import json | |
| class UserSerializer(): | |
| model = User | |
| fields = ['first_name', 'last_name'] | |
| def get_queryset(self): | |
| return self.model.objects.all() | |
| @property | |
| def data(self): | |
| users = self.get_queryset() | |
| for user in users: | |
| groups = list(user.groups.values('id', 'name')) | |
| user['groups'] = groups | |
| user['is_birthday_soon'] = user.is_birthday_in_this_month() | |
| return json.dumps(users) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment