为什么我的Django用户模型的密码不存在? [英] Why isn't my Django User Model's Password Hashed?

查看:109
本文介绍了为什么我的Django用户模型的密码不存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django REST框架(DRF)创建一个可以注册新用户的端点。但是,当我使用POST触发创建端点时,新用户将通过串行化程序进行保存,但密码将以数据库中的明文保存。我的序列化程序的代码如下:

  from django.contrib.auth import get_user_model 
from rest_framework import serializers

class UserSerializer(serializers.ModelSerializer):

class Meta:
model = get_user_model()
fields = ['password','username' 'first_name','last_name','email']
read_only_fields = ['is_staff','is_superuser']
write_only_fields = ['password']
/ pre>

请注意,我使用的是Django auth软件包中的默认用户模型,而且我非常高兴与DRF合作!此外,我发现这个问题提供了一个解决方案,但这似乎需要两个数据库交互 - 我不认为这是有效的,但这可能是一个不正确的假设。

问题是DRF将简单地将字段值设置到模型上。因此,密码在密码字段中设置,并保存在数据库中。但是要正确设置密码,您需要调用 set_password()方法,这将执行散列。



有几种方法可以做到这一点,但休息框架v3的最佳方法是覆盖 update() create()方法在您的Serializer。

 类UserSerializer(serializers.ModelSerializer):
#<您的其他UserSerializer的东西在这里>

def create(self,validated_data):
password = validated_data.pop('password',None)
instance = self.Meta.model(** validated_data)
如果密码不是无:
instance.set_password(密码)
instance.save()
返回实例

def update(self,instance,validated_data)对于attr,
,validated_data.items()中的值:
如果attr =='password':
instance.set_password(value)
else:
setattr实例,attr,value)
instance.save()
返回实例

这里有两件事:


  1. 我们的用户 self.Meta.model ,所以如果模型在
    序列化器上更改,它仍然有效(当然,只要它有一个 set_password
    方法)。

  2. 我们在 validated_data 项目迭代,而不是
    的字段,在中排除 ed字段。

此外,此版本的 create 不保存M2M关系。在您的示例中不需要,但如果需要可以添加。您需要从dict中弹出,然后保存模型并进行设置。


I am using the Django REST Framework (DRF) to create an endpoint with which I can register new users. However, when I hit the creation endpoint with a POST, the new user is saved via a serializer, but the password is saved in cleartext in the database. The code for my serializer is as follows:

from django.contrib.auth import get_user_model
from rest_framework import serializers

class UserSerializer(serializers.ModelSerializer):

    class Meta:
        model = get_user_model()
        fields = ['password', 'username', 'first_name', 'last_name', 'email']
        read_only_fields = ['is_staff', 'is_superuser']
        write_only_fields = ['password']

Please note that I am using the default User model from the Django auth package, and that I am very new to working with DRF! Additionally, I have found this question which provides a solution, but this appears to require two database interactions -- I do not believe that this is efficient, but that might be an incorrect assumption on my part.

解决方案

The issue is DRF will simply set the field values onto the model. Therefore, the password is set on the password field, and saved in the database. But to properly set a password, you need to call the set_password() method, that will do the hashing.

There are several ways to do this, but the best way on rest framework v3 is to override the update() and create() methods on your Serializer.

class UserSerializer(serializers.ModelSerializer):
    # <Your other UserSerializer stuff here>

    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

Two things here:

  1. we user self.Meta.model, so if the model is changed on the serializer, it still works (as long as it has a set_password method of course).
  2. we iterate on validated_data items and not the fields, to account for optionally excludeed fields.

Also, this version of create does not save M2M relations. Not needed in your example, but it could be added if required. You would need to pop those from the dict, save the model and set them afterwards.

这篇关于为什么我的Django用户模型的密码不存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆