本文介绍了如何将Userprofile定义到UserDetialsSerializer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要通过以下方式访问 userprofile 实例:

profile = instance.userprofile UserSerializer中的$ c>语句



实例被创建通过:

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



由于 UserSerializer 正在继承 UserDetailsS​​erializer ,我想我应该在 UserDetailsS​​erializer 中定义一个 userprofile

但是我不知道该怎么做?



问题:如何定义 userprofile UserDetailsS​​erializer 中实现上述?



UserSerializer:

  class UserSerializer(UserDetailsS​​erializer):
company_name = serializers.CharField(source =userprofile.company_name )

class Meta(UserDetailsS​​erializer.Meta):
fields = UserDetailsS​​erializer.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)

#获取和更新用户配置文件
profile = instance.userprofile
如果profile_data和company_name:
profile.company_name = com pany_name
profile.save()
返回实例

UserDetailsS​​erializer:

  class UserDetailsS​​erializer(serializers.ModelSerializer):
class Meta:
model = get_user_model ()

fields =('username','email','first_name','last_name')
read_only_fields =('email',)

UserProfile 型号:

  class UserProfile(models.Model):
user = models.OneToOneField(User)
#用户
的自定义字段company_name = models.CharField(max_length = 100)
/ b

$ b

请问是否需要更加清晰?

解决方案

我想我已经回答了类似的一个



在,假定userprofile已经创建,现在可以更新。您只需要一个支票

 #获取并更新用户个人资料
try:
profile = instance.userprofile
除了UserProfile.DoesNotExist:
profile = UserProfile()
如果profile_data和company_name:


I want to be able to access a userprofile instance through :
profile = instance.userprofile statement in UserSerializer

instance is created through:
instance = super(UserSerializer, self).update(instance, validated_data) statement in UserSerializer

Since UserSerializer is inheriting UserDetailsSerializer, i think i should define a userprofile in UserDetailsSerializer.
But i dont know how to do it ?

Question: How to define userprofile in UserDetailsSerializer to achieve the above ?

UserSerializer:

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

UserDetailsSerializer:

class UserDetailsSerializer(serializers.ModelSerializer):
    class Meta:
        model = get_user_model()

        fields = ('username','email', 'first_name', 'last_name')
        read_only_fields = ('email', )

UserProfile model:

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    # custom fields for user
    company_name = models.CharField(max_length=100)

Do ask if more clarity is required?

解决方案

I think I have answered similar one here

In the documentation it is assumed that userprofile was already created and now can be updated. You just need a check

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

这篇关于如何将Userprofile定义到UserDetialsSerializer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 17:14