我正在遵循这篇教程,但是遇到了以下问题,我无法解决:
- 注册用户后,我无法使用该用户登录api,因为密码未加密,在管理员中显示 "Invalid password format or unknown hashing algorithm."
- 当我未登录api时,我无法向“api/accounts”发帖或在可浏览的api中看到表单。
我的代码:
from django.contrib.auth.models import User
from rest_framework import serializers
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('password', 'first_name', 'last_name', 'email')
write_only_fields = ('password',)
def restore_object(self, attrs, instance=None):
# call set_password on user object. Without this
# the password will be stored in plain text.
user = super(UserSerializer, self).restore_object(attrs, instance)
user.set_password(attrs['password']) #somehow not hashing
return user
write_only_fields = ('password',)
被更改为extra_kwargs = {'password': {'write_only': True}, }
。 - Chemical Programmer