我正在尝试创建一个视图,使用auth_rest库在一个请求中更新扩展配置文件数据,但出现以下错误:
Got AttributeError when attempting to get a value for field
phone
on serializerUserSerializer
. The serializer field might be named incorrectly and not match any attribute or key on theUser
instance. Original exception text was: 'User' object has no attribute 'userprofile'.
这是我的模型(UserProfile)
class UserProfile(models.Model):
user = models.OneToOneField(User, related_name='user', on_delete=models.CASCADE)
phone = models.CharField(max_length=15, blank=True)
phone_whatsapp = models.BooleanField(default=False)
def __str__(self):
return self.user.username
这是序列化程序:
class UserSerializer(UserDetailsSerializer):
phone = serializers.CharField(source="userprofile.phone")
phone_whatsapp = serializers.BooleanField(source="userprofile.phone_whatsapp")
class Meta(UserDetailsSerializer.Meta):
fields = UserDetailsSerializer.Meta.fields + ('phone','phone_whatsapp',)
def update(self, instance, validated_data):
profile_data = validated_data.pop('userprofile', {})
phone = profile_data.get('phone')
phone_whatsapp = profile_data.get('phone_whatsapp')
instance = super(UserSerializer, self).update(instance, validated_data)
# get and update user profile
profile = instance.userprofile
if profile_data and phone and phone_whatsapp:
profile.phone = phone
profile.phone_whatsapp = phone_whatsapp
profile.save()
return instance
我修改了一些东西,但我无法修复它。有人能帮我吗
提前谢谢
目前没有回答
相关问题 更多 >
编程相关推荐