如何在Django Rest Framework中散列Django用户密码?

时间:2016-12-26 14:15:53

标签: python django authentication passwords django-rest-framework

我正在尝试使用Django Rest Framework为我的用户注册创建API。我按照api-guide

中的步骤创建了一个序列化程序
class CreateUserSerializer(serializers.ModelSerializer):
  class Meta:
    model = User
    fields = ('email', 'username', 'password')
    extra_kwargs = {'password': {'write_only': True}}

  def create(self, validated_data):
    user = User(
        email=validated_data['email'],
        username=validated_data['username']
    )
    user.set_password(validated_data['password'])
    user.save()
    return user

但是,我一直为新创建的用户获取Invalid password format or unknown hashing algorithm.。我尝试使用make_password中的django.contrib.auth.hashers,但我仍然无法解决此问题。

由于

6 个答案:

答案 0 :(得分:12)

你可以这样试试

from django.contrib.auth.hashers import make_password

user = User.objects.create(
       email=validated_data['email'],
       username=validated_data['username'],
       password = make_password(validated_data['password'])
)

答案 1 :(得分:4)

您可以在CreateAPIView

中覆盖perform_create方法
from rest_framework.generics import CreateAPIView

class SignUpView(CreateAPIView):
    serializer_class = SignUpSerializers

    def perform_create(self, serializer):
        instance = serializer.save()
        instance.set_password(instance.password)
        instance.save()

答案 2 :(得分:0)

您还可以通过向序列化器添加password方法并使其返回哈希值,来为validate_password字段使用field validation function

from rest_framework.serializers import ModelSerializer
from django.contrib.auth.hashers import make_password


class UserSerializer(ModelSerializer):
    class Meta:
        model = backend.models.User
        fields = ('username', 'email', 'password',)

    validate_password = make_password

答案 3 :(得分:0)

您可以在视图perform_create方法中执行此操作:

from django.contrib.auth.hashers import make_password



def perform_create(self, instance):
        current_user = self.request.user
        user_exists = CustomUser.objects.filter(
            email=self.request.data['email']).first()
        if user_exists:
            raise MethodNotAllowed
        else:
            instance.save(is_active=False, is_confirmed=False,
                          password=make_password(self.request.data['password']))

答案 4 :(得分:0)

在序列化程序中,重新定义使用以下命令创建的函数:

from django.contrib.auth.hashers import make_password

class UserSerializer(ModelSerializer):

    def create(self, validated_data):
        validated_data['password'] = make_password(validated_data['password'])
        return super(UserSerializer, self).create(validated_data)

这一切! :D

答案 5 :(得分:0)

我知道这是一个老话题,但是我今天遇到了这个问题,我想在这里分享我的解决方案。

您可以像下面这样简单地定义序列化器:

class CreateUserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('email', 'username', 'password')
        extra_kwargs = {'password': {'write_only': True}}
另一方面,在您的视图中,您可以覆盖perform_create方法,如下所示:

class UserView(ViewSets.ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserCreateSerializer

    def perform_create(self , serializer):
        new_user = 
        User.objects.create(username=self.request.data.get("username"))
        new_user.set_password(self.request.data.get("password"))
        serializer.save(password=user.password)

通过这种方式,您可以将额外的信息传递给序列化程序进行保存。