使用 Django REST Framework 创建用户 - 不进行身份验证
creating users with Django REST Framework - not authenticate
我正在与 Django 用户一起工作,当我使用 Django REST Framework
创建用户时我已经对密码进行了哈希处理,并且我覆盖了序列化程序上的 create
和 update
方法以散列我的密码用户
class UserSerializer(serializers.ModelSerializer):
#username = models.CharField()
def create(self, validated_data):
password = validated_data.pop('password', None)
instance = self.Meta.model(**validated_data)
if password is not None:
instance.set_password(password)
instance.save()
return instance
def update(self, instance, validated_data):
for attr, value in validated_data.items():
if attr == 'password':
instance.set_password(value)
else:
setattr(instance, attr, value)
instance.save()
return instance
class Meta:
model = User
fields = ('url', 'username', 'password', 'first_name','last_name',
'age', 'sex', 'photo', 'email', 'is_player', 'team',
'position', 'is_staff', 'is_active', 'is_superuser',
'is_player', 'weight', 'height', 'nickname',
'number_matches', 'accomplished_matches',
'time_available', 'leg_profile', 'number_shirt_preferred',
'team_support', 'player_preferred', 'last_login',
)
我的views.py是这样的:
class UserViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to be viewed or edited.
"""
queryset = User.objects.all().order_by('-date_joined')
serializer_class = UserSerializer
filter_fields = ('username', 'is_player', 'first_name', 'last_name', 'team' , 'email', )
我的 REST_FRAMEWORK 设置是:
REST_FRAMEWORK = {
'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',),
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.AllowAny',
),
'PAGE_SIZE': 10
}
我的不便之处在于,当我通过 rest 框架创建和用户时,密码被散列,但我无法通过 rest 身份验证和 Django admin 登录或登录。
如何散列我的密码并通过 Djago REST Framework 登录?
此致
添加 rest framework 身份验证设置也如下
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
)
参考 http://www.django-rest-framework.org/api-guide/authentication/#sessionauthentication
并通过文档进行令牌验证 http://www.django-rest-framework.org/api-guide/authentication/#tokenauthentication
我正在与 Django 用户一起工作,当我使用 Django REST Framework
创建用户时我已经对密码进行了哈希处理,并且我覆盖了序列化程序上的 create
和 update
方法以散列我的密码用户
class UserSerializer(serializers.ModelSerializer):
#username = models.CharField()
def create(self, validated_data):
password = validated_data.pop('password', None)
instance = self.Meta.model(**validated_data)
if password is not None:
instance.set_password(password)
instance.save()
return instance
def update(self, instance, validated_data):
for attr, value in validated_data.items():
if attr == 'password':
instance.set_password(value)
else:
setattr(instance, attr, value)
instance.save()
return instance
class Meta:
model = User
fields = ('url', 'username', 'password', 'first_name','last_name',
'age', 'sex', 'photo', 'email', 'is_player', 'team',
'position', 'is_staff', 'is_active', 'is_superuser',
'is_player', 'weight', 'height', 'nickname',
'number_matches', 'accomplished_matches',
'time_available', 'leg_profile', 'number_shirt_preferred',
'team_support', 'player_preferred', 'last_login',
)
我的views.py是这样的:
class UserViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to be viewed or edited.
"""
queryset = User.objects.all().order_by('-date_joined')
serializer_class = UserSerializer
filter_fields = ('username', 'is_player', 'first_name', 'last_name', 'team' , 'email', )
我的 REST_FRAMEWORK 设置是:
REST_FRAMEWORK = {
'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',),
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.AllowAny',
),
'PAGE_SIZE': 10
}
我的不便之处在于,当我通过 rest 框架创建和用户时,密码被散列,但我无法通过 rest 身份验证和 Django admin 登录或登录。
如何散列我的密码并通过 Djago REST Framework 登录?
此致
添加 rest framework 身份验证设置也如下
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
)
参考 http://www.django-rest-framework.org/api-guide/authentication/#sessionauthentication
并通过文档进行令牌验证 http://www.django-rest-framework.org/api-guide/authentication/#tokenauthentication