Django rest framework 如何在django中将userprofile添加到UserDetailsSerializer

Django rest framework 如何在django中将userprofile添加到UserDetailsSerializer,django-rest-framework,user-profile,django-serializer,Django Rest Framework,User Profile,Django Serializer,正在尝试将userprofile添加到user模型 使用:django rest框架rest身份验证模块 但是lineprofile=instance.userprofile给出错误:***django.db.models.fields.related.relatedObjectsDoetExist:用户没有用户配置文件。 遵循来自的说明 另外,不确定super语句中发生了什么 可能的错误: 1.instance在super语句之后没有userprofile,因此profile=instance

正在尝试将
userprofile
添加到
user
模型

使用
django rest框架
rest身份验证模块

但是line
profile=instance.userprofile
给出错误
***django.db.models.fields.related.relatedObjectsDoetExist:用户没有用户配置文件。

遵循来自的说明

另外,不确定
super
语句中发生了什么

可能的错误:
1.
instance
super
语句之后没有
userprofile
,因此
profile=instance.userprofile
语句给出错误
2.
userprofile
需要添加到
UserDetailsSerializer

UserDetailsSerializer

class UserDetailsSerializer(serializers.ModelSerializer):
    class Meta:
        model = get_user_model()
        fields = ('username', 'email', 'first_name', 'last_name')
        read_only_fields = ('email', )
class UserSerializer(UserDetailsSerializer):

    company_name = serializers.CharField(source="userprofile.company_name")

    class Meta(UserDetailsSerializer.Meta):
        fields = UserDetailsSerializer.Meta.fields + ('company_name',)

    def update(self, instance, validated_data):
        profile_data = validated_data.pop('userprofile', {})
        company_name = profile_data.get('company_name')

        instance = super(UserSerializer, self).update(instance, validated_data)

        # get and update user profile
        profile = instance.userprofile
        if profile_data and company_name:
            profile.company_name = company_name
            profile.save()
        return instance
UserSerializer

class UserDetailsSerializer(serializers.ModelSerializer):
    class Meta:
        model = get_user_model()
        fields = ('username', 'email', 'first_name', 'last_name')
        read_only_fields = ('email', )
class UserSerializer(UserDetailsSerializer):

    company_name = serializers.CharField(source="userprofile.company_name")

    class Meta(UserDetailsSerializer.Meta):
        fields = UserDetailsSerializer.Meta.fields + ('company_name',)

    def update(self, instance, validated_data):
        profile_data = validated_data.pop('userprofile', {})
        company_name = profile_data.get('company_name')

        instance = super(UserSerializer, self).update(instance, validated_data)

        # get and update user profile
        profile = instance.userprofile
        if profile_data and company_name:
            profile.company_name = company_name
            profile.save()
        return instance
如果需要,请务必要求更清晰。
提前感谢。

在中,假定用户配置文件已经创建,现在可以更新。你只需要一张支票

# get and update user profile
    try:
        profile = instance.userprofile
    except UserProfile.DoesNotExist:
        profile = UserProfile()
    if profile_data and company_name:

答案不会马上出现。你正在尝试更新一个不存在的配置文件对象。是的,我理解。这就是为什么,我想知道如何将这个
userprofile
添加到
UserDetailsSerializer
中。我已经修改了这个问题