Python 尝试获取序列化程序`UserSerializer`上字段`phone`的值时获取AttributeError`
我正在尝试创建一个视图,使用auth_rest库在一个请求中更新扩展配置文件数据,但出现以下错误: 尝试获取上的字段<代码>电话的值时获取AttributeError 序列化程序Python 尝试获取序列化程序`UserSerializer`上字段`phone`的值时获取AttributeError`,python,django,django-rest-framework,Python,Django,Django Rest Framework,我正在尝试创建一个视图,使用auth_rest库在一个请求中更新扩展配置文件数据,但出现以下错误: 尝试获取上的字段电话的值时获取AttributeError 序列化程序UserSerializer。序列化程序字段可能已命名 不正确,并且与用户实例上的任何属性或键都不匹配。 原始异常文本为:“用户”对象没有属性 “用户配置文件” 这是我的模型(UserProfile) 这是序列化程序: class UserSerializer(UserDetailsSerializer): phone
UserSerializer
。序列化程序字段可能已命名
不正确,并且与用户
实例上的任何属性或键都不匹配。
原始异常文本为:“用户”对象没有属性
“用户配置文件”
这是我的模型(UserProfile)
这是序列化程序:
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
我修改了一些东西,但我无法修复它。有人能帮我吗
提前感谢我删除了UserProfile'model上的“related_name='user'”。。。根据。。进行迁移。。而且有效!!
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