Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 尝试获取序列化程序`UserSerializer`上字段`phone`的值时获取AttributeError`_Python_Django_Django Rest Framework - Fatal编程技术网

Python 尝试获取序列化程序`UserSerializer`上字段`phone`的值时获取AttributeError`

Python 尝试获取序列化程序`UserSerializer`上字段`phone`的值时获取AttributeError`,python,django,django-rest-framework,Python,Django,Django Rest Framework,我正在尝试创建一个视图,使用auth_rest库在一个请求中更新扩展配置文件数据,但出现以下错误: 尝试获取上的字段电话的值时获取AttributeError 序列化程序UserSerializer。序列化程序字段可能已命名 不正确,并且与用户实例上的任何属性或键都不匹配。 原始异常文本为:“用户”对象没有属性 “用户配置文件” 这是我的模型(UserProfile) 这是序列化程序: class UserSerializer(UserDetailsSerializer): phone

我正在尝试创建一个视图,使用auth_rest库在一个请求中更新扩展配置文件数据,但出现以下错误:

尝试获取上的字段<代码>电话的值时获取AttributeError 序列化程序
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