gpt4 book ai didi

python - 如何在序列化程序中提及密码字段?

转载 作者:行者123 更新时间:2023-12-02 05:29:44 26 4
gpt4 key购买 nike

我有一个 自定义用户 用于身份验证并想为其创建一个序列化程序类,我的自定义用户模型是这样的:

class User (AbstractUser):
bio = models.TextField(max_length=500, blank=True)
birth_date = models.DateField(null=True, blank=True)
image=models.FileField(null=True , blank=True)

我的序列化程序是:
class UserSerializer (serializers.ModelSerializer):
class Meta:
model = User
fields = ('username' ,'email' ,'password' ,'firstname' , 'last name' )

我怎么能提到 密码字段 是密码,其内容必须经过哈希处理?

最佳答案

要散列密码,请调用:

make_password(origin_password)

示例 序列化程序.py :
from rest_framework import serializers
from django.contrib.auth.models import User
from django.contrib.auth.hashers import make_password


class UserSerializer(serializers.HyperlinkedModelSerializer):

password = serializers.CharField(
write_only=True,
required=True,
help_text='Leave empty if no change needed',
style={'input_type': 'password', 'placeholder': 'Password'}
)

class Meta:
model = User
fields = ('url', 'username', 'email', 'password')

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

关于python - 如何在序列化程序中提及密码字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49189484/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com