![](/img/trans.png)
[英]django how to create hashed password using create_user() method
[英]create new user in django gives not hashed password
我試圖在django rest框架內使用post請求創建用戶。
這是我的序列化代碼:
class UserSerializer(serializers.ModelSerializer):
favorites = serializers.PrimaryKeyRelatedField(many=True, queryset=Favorite.objects.all())
class Meta:
model = User
fields = ('id', 'username','password','favorites')
extra_kwargs = {
'password': {
'write_only': True,
},
'confirm_password': {
'write_only': True,
},
'favorites':
{
'read_only': True,
},
}
def create(self, validated_data):
return User.objects.create(**validated_data)
對於create方法,我也使用了這個實現:
def create(self, validated_data):
user = User(username=validated_data['username'])
user.set_password(validated_data['password'])
user.save()
return user
在我的管理面板中,它為創建的用戶說:
Invalid password format or unknown hashing algorithm.
我應該改變什么?
User
的管理器上有一個特殊的方法 - create_user() ,它可以完全滿足你的需要
這個答案修正了它: https : //stackoverflow.com/a/29391122/355641
我想使用get_user_model
是解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.