forked from AltSchool/dynamic-rest
-
Notifications
You must be signed in to change notification settings - Fork 1
/
drest.py
62 lines (41 loc) · 1.25 KB
/
drest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from dynamic_rest import fields as fields
from dynamic_rest import routers as routers
from dynamic_rest import serializers as serializers
from dynamic_rest import viewsets as viewsets
from .models import Group, Permission, User
# DREST
# DREST serializers
class UserSerializer(serializers.DynamicModelSerializer):
class Meta:
model = User
name = 'user'
fields = ('id', 'name', 'groups')
groups = fields.DynamicRelationField(
'GroupSerializer',
embed=True,
many=True,
deferred=True
)
class GroupSerializer(serializers.DynamicModelSerializer):
class Meta:
model = Group
name = 'group'
fields = ('id', 'name', 'permissions')
permissions = fields.DynamicRelationField(
'PermissionSerializer',
embed=True,
many=True,
deferred=True
)
class PermissionSerializer(serializers.DynamicModelSerializer):
class Meta:
model = Permission
name = 'permission'
fields = ('id', 'name')
# DREST views
class UserViewSet(viewsets.DynamicModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
# DREST router
router = routers.DynamicRouter()
router.register(r'drest/users', UserViewSet)