【问题标题】:Django Rest not creating extended UserProfileDjango Rest 没有创建扩展的 UserProfile
【发布时间】:2015-05-18 08:48:31
【问题描述】:

我正在使用 Django Rest Framework,并创建了一个扩展的 UserProfile 模型,如下所示:

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    #Some Fields for UserProfile

    def user_profile_url(self):
        return reverse('user_profile', args=(self.user.id, "{}-{}".format(self.user.first_name, self.user.last_name)))

User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])

但是,在使用rest_auth/registration 端点:http://django-rest-auth.readthedocs.org/en/latest/api_endpoints.html#registration 注册时,即使创建了User,也不会创建用户配置文件。在我的serializers.py 中,我为注册的用户做了以下操作

class UserSignUpSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('email',)

    def create(self, validated_data):
        user = User(email=validated_data['email'], username=validated_data['email'])
        user.set_password(validated_data['password'])
        user.save()
        profile = UserProfile(user=user)
        profile.save()
        return user

我哪里错了?

【问题讨论】:

  • 嗨@Newtt 你得到解决方案了吗?有的话请贴在这里。

标签: python django rest django-rest-auth


【解决方案1】:

因为请求在这里https://github.com/Tivix/django-rest-auth/blob/master/rest_auth/registration/views.py#L38 并没有调用 serializer.create() 实际上。

尝试按照文档中的建议覆盖注册表单:

ACCOUNT_FORMS = {
   'signup': 'path.to.custom.SignupForm'
}

个人资料表格示例: https://djangosnippets.org/snippets/2081/

【讨论】:

  • 使用上面的表格。它显示了这个错误: RemovedInDjango18Warning: Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is deprecated - form ProfileForm needs updates class ProfileForm(forms.ModelForm):
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多