【发布时间】: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