-
Notifications
You must be signed in to change notification settings - Fork 111
/
serializers.py
325 lines (260 loc) · 8.42 KB
/
serializers.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
from rest_framework.serializers import CharField
from dynamic_rest.fields import (
CountField,
DynamicField,
DynamicGenericRelationField,
DynamicMethodField,
DynamicRelationField
)
from dynamic_rest.serializers import (
DynamicEphemeralSerializer,
DynamicModelSerializer
)
from tests.models import (
Car,
Cat,
Country,
Dog,
Group,
Horse,
Location,
Part,
Permission,
Profile,
User,
Zebra,
)
def backup_home_link(name, field, data, obj):
return "/locations/%s/?include[]=address" % obj.backup_home_id
class CatSerializer(DynamicModelSerializer):
home = DynamicRelationField('LocationSerializer', link=None)
backup_home = DynamicRelationField(
'LocationSerializer', link=backup_home_link)
foobar = DynamicRelationField(
'LocationSerializer', source='hunting_grounds', many=True)
parent = DynamicRelationField('CatSerializer', immutable=True)
class Meta:
model = Cat
name = 'cat'
fields = ('id', 'name', 'home', 'backup_home', 'foobar', 'parent')
deferred_fields = ('home', 'backup_home', 'foobar', 'parent')
immutable_fields = ('name',)
untrimmed_fields = ('name',)
class LocationSerializer(DynamicModelSerializer):
class Meta:
defer_many_relations = False
model = Location
name = 'location'
fields = (
'id', 'name', 'users', 'user_count', 'address',
'cats', 'friendly_cats', 'bad_cats'
)
users = DynamicRelationField(
'UserSerializer',
source='user_set',
many=True,
deferred=True
)
user_count = CountField('users', required=False, deferred=True)
address = DynamicField(source='blob', required=False, deferred=True)
cats = DynamicRelationField(
'CatSerializer', source='cat_set', many=True, deferred=True)
friendly_cats = DynamicRelationField(
'CatSerializer', many=True, deferred=True)
bad_cats = DynamicRelationField(
'CatSerializer', source='annoying_cats', many=True, deferred=True)
def filter_queryset(self, query):
return query.exclude(name='Atlantis')
class PermissionSerializer(DynamicModelSerializer):
class Meta:
defer_many_relations = True
model = Permission
name = 'permission'
fields = ('id', 'name', 'code', 'users', 'groups')
deferred_fields = ('code',)
users = DynamicRelationField('UserSerializer', many=True, deferred=False)
groups = DynamicRelationField('GroupSerializer', many=True)
class GroupSerializer(DynamicModelSerializer):
class Meta:
model = Group
name = 'group'
fields = (
'id',
'name',
'permissions',
'members',
'users',
'loc1users',
'loc1usersLambda'
)
permissions = DynamicRelationField(
'PermissionSerializer',
many=True,
deferred=True)
members = DynamicRelationField(
'UserSerializer',
source='users',
many=True,
deferred=True)
# Intentional duplicate of 'users':
users = DynamicRelationField(
'UserSerializer',
many=True,
deferred=True)
# For testing default queryset on relations:
loc1users = DynamicRelationField(
'UserSerializer',
source='users',
many=True,
queryset=User.objects.filter(location_id=1),
deferred=True)
loc1usersLambda = DynamicRelationField(
'UserSerializer',
source='users',
many=True,
queryset=lambda srlzr: User.objects.filter(location_id=1),
deferred=True)
class UserSerializer(DynamicModelSerializer):
class Meta:
model = User
name = 'user'
fields = (
'id',
'name',
'permissions',
'groups',
'location',
'last_name',
'display_name',
'thumbnail_url',
'number_of_cats',
'profile',
'date_of_birth',
'favorite_pet_id',
'favorite_pet',
'is_dead',
)
deferred_fields = (
'last_name',
'date_of_birth',
'display_name',
'profile',
'thumbnail_url',
'favorite_pet_id',
'favorite_pet',
'is_dead',
)
read_only_fields = ('profile',)
location = DynamicRelationField('LocationSerializer')
permissions = DynamicRelationField(
'PermissionSerializer',
many=True,
deferred=True
)
groups = DynamicRelationField('GroupSerializer', many=True, deferred=True)
display_name = DynamicField(source='profile.display_name', read_only=True)
thumbnail_url = DynamicField(
source='profile.thumbnail_url',
read_only=True
)
number_of_cats = DynamicMethodField(
requires=['location.cat_set.*'],
deferred=True
)
# Don't set read_only on this field directly. Used in test for
# Meta.read_only_fields.
profile = DynamicRelationField(
'ProfileSerializer',
deferred=True
)
favorite_pet = DynamicGenericRelationField(required=False)
def get_number_of_cats(self, user):
if not self.context.get('request'):
# Used in test_api.py::test_relation_includes_context
raise Exception("No request object in context")
location = user.location
return len(location.cat_set.all()) if location else 0
class ProfileSerializer(DynamicModelSerializer):
class Meta:
model = Profile
name = 'profile'
fields = (
'user',
'display_name',
'thumbnail_url',
'user_location_name',
)
user = DynamicRelationField('UserSerializer')
user_location_name = DynamicField(
source='user.location.name',
requires=['user.location.name'],
read_only=True
)
class LocationGroupSerializer(DynamicEphemeralSerializer):
class Meta:
name = 'locationgroup'
id = DynamicField(field_type=str)
location = DynamicRelationField('LocationSerializer', deferred=False)
groups = DynamicRelationField('GroupSerializer', many=True, deferred=False)
class CountsSerializer(DynamicEphemeralSerializer):
class Meta:
name = 'counts'
values = DynamicField(field_type=list)
count = CountField('values', unique=False)
unique_count = CountField('values')
class NestedEphemeralSerializer(DynamicEphemeralSerializer):
class Meta:
name = 'nested'
value_count = DynamicRelationField('CountsSerializer', deferred=False)
class UserLocationSerializer(UserSerializer):
""" Serializer to test embedded fields """
class Meta:
model = User
name = 'user_location'
fields = ('groups', 'location', 'id')
location = DynamicRelationField('LocationSerializer', embed=True)
groups = DynamicRelationField('GroupSerializer', many=True, embed=True)
class DogSerializer(DynamicModelSerializer):
class Meta:
model = Dog
fields = ('id', 'name', 'origin', 'fur', 'is_red')
fur = CharField(source='fur_color')
is_red = DynamicMethodField(deferred=True, requires=['fur_color'])
def get_is_red(self, instance):
return instance.fur_color == 'red'
class HorseSerializer(DynamicModelSerializer):
class Meta:
model = Horse
name = 'horse'
fields = (
'id',
'name',
'origin',
)
class ZebraSerializer(DynamicModelSerializer):
class Meta:
model = Zebra
name = 'zebra'
fields = (
'id',
'name',
'origin',
)
class CountrySerializer(DynamicModelSerializer):
class Meta:
model = Country
fields = ('id', 'name', 'short_name')
deferred_fields = ('name', 'short_name')
class PartSerializer(DynamicModelSerializer):
country = DynamicRelationField('CountrySerializer')
class Meta:
model = Part
fields = ('id', 'name', 'country')
deferred_fields = ('name', 'country')
class CarSerializer(DynamicModelSerializer):
country = DynamicRelationField('CountrySerializer')
parts = DynamicRelationField('PartSerializer', many=True, source='part_set') # noqa
class Meta:
model = Car
fields = ('id', 'name', 'country', 'parts')
deferred_fields = ('name', 'country', 'parts')