@@ -195,7 +195,7 @@ def get_default(self):
195
195
raise SkipField ()
196
196
return self .default
197
197
198
- def validate_value (self , data = empty ):
198
+ def run_validation (self , data = empty ):
199
199
"""
200
200
Validate a simple representation and return the internal value.
201
201
@@ -208,7 +208,7 @@ def validate_value(self, data=empty):
208
208
self .fail ('required' )
209
209
return self .get_default ()
210
210
211
- value = self .to_native (data )
211
+ value = self .to_internal_value (data )
212
212
self .run_validators (value )
213
213
return value
214
214
@@ -225,17 +225,17 @@ def run_validators(self, value):
225
225
if errors :
226
226
raise ValidationError (errors )
227
227
228
- def to_native (self , data ):
228
+ def to_internal_value (self , data ):
229
229
"""
230
230
Transform the *incoming* primative data into a native value.
231
231
"""
232
- raise NotImplementedError ('to_native () must be implemented.' )
232
+ raise NotImplementedError ('to_internal_value () must be implemented.' )
233
233
234
- def to_primative (self , value ):
234
+ def to_representation (self , value ):
235
235
"""
236
236
Transform the *outgoing* native value into primative data.
237
237
"""
238
- raise NotImplementedError ('to_primative () must be implemented.' )
238
+ raise NotImplementedError ('to_representation () must be implemented.' )
239
239
240
240
def fail (self , key , ** kwargs ):
241
241
"""
@@ -279,14 +279,14 @@ def get_value(self, dictionary):
279
279
return dictionary .get (self .field_name , False )
280
280
return dictionary .get (self .field_name , empty )
281
281
282
- def to_native (self , data ):
282
+ def to_internal_value (self , data ):
283
283
if data in self .TRUE_VALUES :
284
284
return True
285
285
elif data in self .FALSE_VALUES :
286
286
return False
287
287
self .fail ('invalid' , input = data )
288
288
289
- def to_primative (self , value ):
289
+ def to_representation (self , value ):
290
290
if value is None :
291
291
return None
292
292
if value in self .TRUE_VALUES :
@@ -309,12 +309,14 @@ def __init__(self, **kwargs):
309
309
self .min_length = kwargs .pop ('min_length' , None )
310
310
super (CharField , self ).__init__ (** kwargs )
311
311
312
- def to_native (self , data ):
312
+ def to_internal_value (self , data ):
313
313
if data == '' and not self .allow_blank :
314
314
self .fail ('blank' )
315
+ if data is None :
316
+ return None
315
317
return str (data )
316
318
317
- def to_primative (self , value ):
319
+ def to_representation (self , value ):
318
320
if value is None :
319
321
return None
320
322
return str (value )
@@ -326,17 +328,17 @@ class EmailField(CharField):
326
328
}
327
329
default_validators = [validators .validate_email ]
328
330
329
- def to_native (self , data ):
330
- ret = super (EmailField , self ).to_native (data )
331
- if ret is None :
331
+ def to_internal_value (self , data ):
332
+ if data == '' and not self .allow_blank :
333
+ self .fail ('blank' )
334
+ if data is None :
332
335
return None
333
- return ret .strip ()
336
+ return str ( data ) .strip ()
334
337
335
- def to_primative (self , value ):
336
- ret = super (EmailField , self ).to_primative (value )
337
- if ret is None :
338
+ def to_representation (self , value ):
339
+ if value is None :
338
340
return None
339
- return ret .strip ()
341
+ return str ( value ) .strip ()
340
342
341
343
342
344
class RegexField (CharField ):
@@ -378,14 +380,14 @@ def __init__(self, **kwargs):
378
380
if min_value is not None :
379
381
self .validators .append (validators .MinValueValidator (min_value ))
380
382
381
- def to_native (self , data ):
383
+ def to_internal_value (self , data ):
382
384
try :
383
385
data = int (str (data ))
384
386
except (ValueError , TypeError ):
385
387
self .fail ('invalid' )
386
388
return data
387
389
388
- def to_primative (self , value ):
390
+ def to_representation (self , value ):
389
391
if value is None :
390
392
return None
391
393
return int (value )
@@ -405,19 +407,19 @@ def __init__(self, **kwargs):
405
407
if min_value is not None :
406
408
self .validators .append (validators .MinValueValidator (min_value ))
407
409
408
- def to_primative (self , value ):
410
+ def to_internal_value (self , value ):
411
+ if value is None :
412
+ return None
413
+ return float (value )
414
+
415
+ def to_representation (self , value ):
409
416
if value is None :
410
417
return None
411
418
try :
412
419
return float (value )
413
420
except (TypeError , ValueError ):
414
421
self .fail ('invalid' , value = value )
415
422
416
- def to_native (self , value ):
417
- if value is None :
418
- return None
419
- return float (value )
420
-
421
423
422
424
class DecimalField (Field ):
423
425
default_error_messages = {
@@ -439,7 +441,7 @@ def __init__(self, max_digits, decimal_places, coerce_to_string=True, max_value=
439
441
if min_value is not None :
440
442
self .validators .append (validators .MinValueValidator (min_value ))
441
443
442
- def from_native (self , value ):
444
+ def to_internal_value (self , value ):
443
445
"""
444
446
Validates that the input is a decimal number. Returns a Decimal
445
447
instance. Returns None for empty values. Ensures that there are no more
@@ -485,7 +487,7 @@ def from_native(self, value):
485
487
486
488
return value
487
489
488
- def to_primative (self , value ):
490
+ def to_representation (self , value ):
489
491
if isinstance (value , decimal .Decimal ):
490
492
context = decimal .getcontext ().copy ()
491
493
context .prec = self .max_digits
@@ -516,7 +518,7 @@ def __init__(self, input_formats=None, format=None, *args, **kwargs):
516
518
self .format = format if format is not None else self .format
517
519
super (DateField , self ).__init__ (* args , ** kwargs )
518
520
519
- def from_native (self , value ):
521
+ def to_internal_value (self , value ):
520
522
if value in validators .EMPTY_VALUES :
521
523
return None
522
524
@@ -552,7 +554,7 @@ def from_native(self, value):
552
554
msg = self .error_messages ['invalid' ] % humanized_format
553
555
raise ValidationError (msg )
554
556
555
- def to_primative (self , value ):
557
+ def to_representation (self , value ):
556
558
if value is None or self .format is None :
557
559
return value
558
560
@@ -576,7 +578,7 @@ def __init__(self, input_formats=None, format=None, *args, **kwargs):
576
578
self .format = format if format is not None else self .format
577
579
super (DateTimeField , self ).__init__ (* args , ** kwargs )
578
580
579
- def from_native (self , value ):
581
+ def to_internal_value (self , value ):
580
582
if value in validators .EMPTY_VALUES :
581
583
return None
582
584
@@ -618,7 +620,7 @@ def from_native(self, value):
618
620
msg = self .error_messages ['invalid' ] % humanized_format
619
621
raise ValidationError (msg )
620
622
621
- def to_primative (self , value ):
623
+ def to_representation (self , value ):
622
624
if value is None or self .format is None :
623
625
return value
624
626
@@ -670,7 +672,7 @@ def from_native(self, value):
670
672
msg = self .error_messages ['invalid' ] % humanized_format
671
673
raise ValidationError (msg )
672
674
673
- def to_primative (self , value ):
675
+ def to_representation (self , value ):
674
676
if value is None or self .format is None :
675
677
return value
676
678
@@ -711,13 +713,13 @@ def __init__(self, choices, **kwargs):
711
713
712
714
super (ChoiceField , self ).__init__ (** kwargs )
713
715
714
- def to_native (self , data ):
716
+ def to_internal_value (self , data ):
715
717
try :
716
718
return self .choice_strings_to_values [str (data )]
717
719
except KeyError :
718
720
self .fail ('invalid_choice' , input = data )
719
721
720
- def to_primative (self , value ):
722
+ def to_representation (self , value ):
721
723
return value
722
724
723
725
@@ -727,15 +729,15 @@ class MultipleChoiceField(ChoiceField):
727
729
'not_a_list' : _ ('Expected a list of items but got type `{input_type}`' )
728
730
}
729
731
730
- def to_native (self , data ):
732
+ def to_internal_value (self , data ):
731
733
if not hasattr (data , '__iter__' ):
732
734
self .fail ('not_a_list' , input_type = type (data ).__name__ )
733
735
return set ([
734
- super (MultipleChoiceField , self ).to_native (item )
736
+ super (MultipleChoiceField , self ).to_internal_value (item )
735
737
for item in data
736
738
])
737
739
738
- def to_primative (self , value ):
740
+ def to_representation (self , value ):
739
741
return value
740
742
741
743
@@ -768,7 +770,7 @@ def __init__(self, **kwargs):
768
770
kwargs ['read_only' ] = True
769
771
super (ReadOnlyField , self ).__init__ (** kwargs )
770
772
771
- def to_primative (self , value ):
773
+ def to_representation (self , value ):
772
774
if is_simple_callable (value ):
773
775
return value ()
774
776
return value
@@ -795,7 +797,7 @@ def __init__(self, method_attr=None, **kwargs):
795
797
kwargs ['read_only' ] = True
796
798
super (SerializerMethodField , self ).__init__ (** kwargs )
797
799
798
- def to_primative (self , value ):
800
+ def to_representation (self , value ):
799
801
method_attr = self .method_attr
800
802
if method_attr is None :
801
803
method_attr = 'get_{field_name}' .format (field_name = self .field_name )
@@ -815,13 +817,13 @@ def __init__(self, model_field, **kwargs):
815
817
kwargs ['source' ] = '*'
816
818
super (ModelField , self ).__init__ (** kwargs )
817
819
818
- def to_native (self , data ):
820
+ def to_internal_value (self , data ):
819
821
rel = getattr (self .model_field , 'rel' , None )
820
822
if rel is not None :
821
823
return rel .to ._meta .get_field (rel .field_name ).to_python (data )
822
824
return self .model_field .to_python (data )
823
825
824
- def to_primative (self , obj ):
826
+ def to_representation (self , obj ):
825
827
value = self .model_field ._get_val_from_obj (obj )
826
828
if is_protected_type (value ):
827
829
return value
0 commit comments