[Fixed]-Show first_name, last_name and email in profile serializer

2๐Ÿ‘

โœ…

You show the first_name , last_name and username in ProfileSerializer using source keyword argument in a CharField for first_name and last_name and username.

class ProfileSerializer(serializers.ModelSerializer):
    # url = serializers.HyperlinkedRelatedField(source="user", view_name="user_profile")
    user = serializers.PrimaryKeyRelatedField(read_only=True)
    token = serializers.PrimaryKeyRelatedField(read_only=True)
    first_name = serializers.CharField(source = "user.first_name")
    last_name = serializers.CharField(source = "user.last_name")
    username = serializers.CharField(source = "user.username")

    class Meta:
        model = Profile
        fields = ('token', 'user', 'current_location', 'permanent_location', 'dob',
                    'about_me', 'gender_status', 'create_profile_for', 'marital_status',
                    'height', 'weight', 'body_type', 'complexion',)

    def update(self, instance, validated_data):
        first_name = validated_data.pop('first_name', None)
        last_name = validated_data.pop('last_name', None)
        user_inst_fields = {}
        if first_name:
            user_inst_fields['first_name'] = first_name
        if last_name:
            user_inst_fields['last_name'] = last_name
        if user_inst_fields:
            User.objects.update_or_create(id=instance.user.id, defaults=user_inst_fields)
        profile, created = Profile.objects.update_or_create(token=instance.token, defaults=validated_data)
        print('profile', profile, created)
        return profile

Now , DRF will fetch first_name , last_name and email from the user relation on the Profile model.

๐Ÿ‘คxssChauhan

-1๐Ÿ‘

My solution for this problem, because solution from this topic return error.

class DirectorSerializer(serializers.ModelSerializer):
    user = serializers.PrimaryKeyRelatedField(read_only=True)
    first_name=serializers.CharField(source='user.first_name', ...)
    last_name=serializers.CharField(source='user.last_name',  ...)

    class Meta:
        model= Director
        fields=['user','first_name', 'last_name',
                'description','private_info']
    

    def update(self, instance, validated_data):
        user_data = validated_data.pop('user', None)
        first_name = user_data.get('first_name', instance.user.first_name)
        last_name = user_data.get('last_name', instance.user.last_name)
        
        user_def={
            'first_name': first_name,
            'last_name': last_name,
        }
        User.objects.update_or_create(id=instance.user.id, defaults=user_def)

        description = validated_data.get('description', instance.description)
        private_info = validated_data.get('private_info', instance.private_info)

        director_def={
            'description': description,
            'private_info': private_info,
        }
        director, created = Director.objects.update_or_create(user=instance.user, 
                                                  defaults=director_def)
        return director

        # Or method in more direct way:
        def update(self, instance, validated_data):
            user_data = validated_data.pop('user', None)
            first_name = user_data.get('first_name', instance.user.first_name)
            last_name = user_data.get('last_name', instance.user.last_name)

            instance.user.first_name = first_name
            instance.user.last_name = last_name
            instance.user.save()

            instance.description = validated_data.get('description', instance.description)
            instance.private_info = validated_data.get('private_info', instance.private_info)
            instance.save()

            return instance
๐Ÿ‘คVoitecP

Leave a comment