我正在通过Django all-auth使用Django rest-auth,我想扩展注册功能以匹配我的自定义用户对象。鉴于以下用户模型:
class Profile(AbstractUser):
email = models.EmailField(unique=True)
username = models.CharField(max_length=40, unique=True, blank=True)
first_name = models.CharField(max_length=40, blank=True)
last_name = models.CharField(max_length=40, blank=True)
如何启用allauth通过API调用接收它们作为参数然后清理/存储它们。换句话说,我希望能够做的是用这样的主体调用/rest-auth/registration/
端点;
{
"username": "foo",
"email": "bar@mail.com",
"password1": "fooBar!@",
"password2": "fooBar!@",
"first_name": "first",
"last_name": "last"
}
并将first_name
和last_name
存储为个人资料对象。同样,我希望能够通过API而不是通过表单来实现这一目标。
环境详情:
Django==1.11.3
django-allauth==0.33.0
django-environ==0.4.4
django-rest-auth==0.9.1
django-storages==1.6.5
django-taggit==0.22.1
djangorestframework==3.6.4
...
$ python --version
Python 2.7.11
答案 0 :(得分:1)
尝试覆盖您的注册序列化程序,如下所示:
class CustomRegisterSerializer(RegisterSerializer):
first_name = serializers.CharField(max_length=255)
last_name = serializers.CharField(max_length=255)
def get_cleaned_data(self):
validated_data = super(CustomRegisterSerializer, self).get_cleaned_data()
validated_data.update({
'first_name': self.validated_data.get('first_name', ''),
'last_name': self.validated_data.get('last_name', '')
})
return validated_data
然后将此序列化程序添加到您的设置中REGISTER_SERIALIZER
(查看文档了解详情)