3
3
4
4
from mongoengine import fields as me_fields
5
5
from mongoengine .errors import ValidationError as me_ValidationError
6
-
7
- from rest_framework import serializers
8
6
from rest_framework import fields as drf_fields
9
- from rest_framework . utils . field_mapping import ClassLookupDict
7
+ from rest_framework import serializers
10
8
from rest_framework .compat import unicode_to_repr
9
+ from rest_framework .utils .field_mapping import ClassLookupDict
10
+ from rest_framework_mongoengine .validators import (UniqueTogetherValidator ,
11
+ UniqueValidator )
11
12
12
- from rest_framework_mongoengine .validators import UniqueValidator , UniqueTogetherValidator
13
-
14
-
15
- from .fields import (ObjectIdField ,
16
- DocumentField ,
17
- GenericEmbeddedDocumentField ,
18
- DynamicField ,
19
- ReferenceField ,
20
- GenericReferenceField ,
21
- GeoPointField ,
22
- GeoJSONField )
23
-
24
- from .utils import (is_abstract_model ,
25
- get_field_info ,
26
- get_field_kwargs ,
27
- get_relation_kwargs ,
28
- has_default ,
29
- COMPOUND_FIELD_TYPES )
30
-
13
+ from .fields import (DocumentField , DynamicField , GenericEmbeddedDocumentField ,
14
+ GenericReferenceField , GeoJSONField , GeoPointField ,
15
+ ObjectIdField , ReferenceField )
31
16
from .repr import serializer_repr
17
+ from .utils import (COMPOUND_FIELD_TYPES , get_field_info , get_field_kwargs ,
18
+ get_relation_kwargs , has_default , is_abstract_model )
32
19
33
20
34
21
def raise_errors_on_nested_writes (method_name , serializer , validated_data ):
@@ -239,7 +226,6 @@ def get_fields(self):
239
226
'Cannot use ModelSerializer with Abstract Models.'
240
227
)
241
228
242
-
243
229
# Retrieve metadata about fields & relationships on the model class.
244
230
self .field_info = get_field_info (model )
245
231
field_names = self .get_field_names (declared_fields , self .field_info )
@@ -277,7 +263,6 @@ def get_fields(self):
277
263
278
264
return fields
279
265
280
-
281
266
def get_default_field_names (self , declared_fields , model_info ):
282
267
return (
283
268
[model_info .pk .name ] +
@@ -291,7 +276,7 @@ def build_field(self, field_name, info, model_class, nested_depth):
291
276
if field_name in info .fields_and_pk :
292
277
model_field = info .fields_and_pk [field_name ]
293
278
if isinstance (model_field , COMPOUND_FIELD_TYPES ):
294
- child_name = field_name + '.child'
279
+ child_name = field_name + '.child'
295
280
if child_name in info .fields or child_name in info .embedded :
296
281
child_class , child_kwargs = self .build_field (child_name , info , model_class , nested_depth )
297
282
child_field = child_class (** child_kwargs )
@@ -314,7 +299,6 @@ def build_field(self, field_name, info, model_class, nested_depth):
314
299
315
300
return self .build_unknown_field (field_name , model_class )
316
301
317
-
318
302
def build_standard_field (self , field_name , model_field ):
319
303
field_mapping = ClassLookupDict (self .serializer_field_mapping )
320
304
@@ -380,7 +364,7 @@ class Meta:
380
364
depth = nested_depth - 1
381
365
382
366
field_class = NestedSerializer
383
- field_kwargs = { 'read_only' : True }
367
+ field_kwargs = {'read_only' : True }
384
368
elif relation_info .related_model :
385
369
field_class = ReferenceField
386
370
field_kwargs = get_relation_kwargs (field_name , relation_info )
@@ -411,7 +395,6 @@ class Meta:
411
395
412
396
return field_class , field_kwargs
413
397
414
-
415
398
def get_uniqueness_extra_kwargs (self , field_names , extra_kwargs ):
416
399
# extra_kwargs contains 'default', 'required', 'validators=[UniqValidator]'
417
400
# hidden_fields contains fields involved in constraints, but missing in serializer fields
@@ -427,7 +410,7 @@ def get_uniqueness_extra_kwargs(self, field_names, extra_kwargs):
427
410
428
411
# include `unique_with` from model indexes
429
412
# so long as all the field names are included on the serializer.
430
- uniq_indexes = filter (lambda i : i .get ('unique' ,False ), model ._meta .get ('index_specs' ,[]))
413
+ uniq_indexes = filter (lambda i : i .get ('unique' , False ), model ._meta .get ('index_specs' , []))
431
414
for idx in uniq_indexes :
432
415
field_set = set (map (lambda e : e [0 ], idx ['fields' ]))
433
416
if field_names .issuperset (field_set ):
@@ -445,9 +428,9 @@ def get_uniqueness_extra_kwargs(self, field_names, extra_kwargs):
445
428
for field_name in unique_together_fields :
446
429
fld = model ._fields [field_name ]
447
430
if has_default (fld ):
448
- uniq_extra_kwargs [field_name ] = { 'default' : fld .default }
431
+ uniq_extra_kwargs [field_name ] = {'default' : fld .default }
449
432
else :
450
- uniq_extra_kwargs [field_name ] = { 'required' : True }
433
+ uniq_extra_kwargs [field_name ] = {'required' : True }
451
434
452
435
# Update `extra_kwargs` with any new options.
453
436
for key , value in uniq_extra_kwargs .items ():
@@ -465,9 +448,9 @@ def get_unique_together_validators(self):
465
448
validators = []
466
449
field_names = set (self .get_field_names (self ._declared_fields , self .field_info ))
467
450
468
- uniq_indexes = filter (lambda i : i .get ('unique' ,False ), model ._meta .get ('index_specs' ,[]))
451
+ uniq_indexes = filter (lambda i : i .get ('unique' , False ), model ._meta .get ('index_specs' , []))
469
452
for idx in uniq_indexes :
470
- if not idx .get ('unique' ,False ):
453
+ if not idx .get ('unique' , False ):
471
454
continue
472
455
field_set = tuple (map (lambda e : e [0 ], idx ['fields' ]))
473
456
if len (field_set ) > 1 and field_names .issuperset (set (field_set )):
@@ -506,7 +489,7 @@ def get_unique_together_validators(self):
506
489
507
490
def to_internal_value (self , data ):
508
491
# run nested validation and convert to document instance
509
- data = super (EmbeddedDocumentSerializer ,self ).to_internal_value (data )
492
+ data = super (EmbeddedDocumentSerializer , self ).to_internal_value (data )
510
493
return self .Meta .model (** data )
511
494
512
495
@@ -516,7 +499,7 @@ class DynamicDocumentSerializer(DocumentSerializer):
516
499
Maps all undefined fields to :class:`fields.DynamicField`.
517
500
"""
518
501
def to_representation (self , instance ):
519
- ret = super (DynamicDocumentSerializer ,self ).to_representation (instance )
502
+ ret = super (DynamicDocumentSerializer , self ).to_representation (instance )
520
503
521
504
for field_name , field in self ._map_dynamic_fields (instance ).items ():
522
505
ret [field_name ] = field .to_representation (field .get_attribute (instance ))
0 commit comments