1- from distutils . version import StrictVersion
1+ from django . contrib . auth import get_user_model
22
3- import rest_framework
43from rest_framework import serializers
5- from django .forms import widgets
64
75
8- DRF_VERSION_INFO = StrictVersion (rest_framework .VERSION ).version
9- DRF2 = DRF_VERSION_INFO [0 ] == 2
10- DRF3 = DRF_VERSION_INFO [0 ] == 3
6+ class Serializer (serializers .Serializer ):
7+ @property
8+ def object (self ):
9+ return self .validated_data
1110
1211
13- if DRF2 :
14- class Serializer (serializers .Serializer ):
15- pass
12+ class PasswordField (serializers .CharField ):
1613
17- class PasswordField (serializers .CharField ):
18- widget = widgets .PasswordInput
19- else :
20- class Serializer (serializers .Serializer ):
21- @property
22- def object (self ):
23- return self .validated_data
24-
25- class PasswordField (serializers .CharField ):
26-
27- def __init__ (self , * args , ** kwargs ):
28- if 'style' not in kwargs :
29- kwargs ['style' ] = {'input_type' : 'password' }
30- else :
31- kwargs ['style' ]['input_type' ] = 'password'
32- super (PasswordField , self ).__init__ (* args , ** kwargs )
33-
34-
35- def get_user_model ():
36- try :
37- from django .contrib .auth import get_user_model
38- except ImportError : # Django < 1.5
39- from django .contrib .auth .models import User
40- else :
41- User = get_user_model ()
42-
43- return User
14+ def __init__ (self , * args , ** kwargs ):
15+ if 'style' not in kwargs :
16+ kwargs ['style' ] = {'input_type' : 'password' }
17+ else :
18+ kwargs ['style' ]['input_type' ] = 'password'
19+ super (PasswordField , self ).__init__ (* args , ** kwargs )
4420
4521
4622def get_username_field ():
@@ -59,11 +35,3 @@ def get_username(user):
5935 username = user .username
6036
6137 return username
62-
63-
64- def get_request_data (request ):
65- if DRF2 :
66- data = request .DATA
67- else :
68- data = request .data
69- return data
0 commit comments