【问题标题】:Write an explicit `.update()` method for serializer为序列化程序编写一个显式的 .update() 方法
【发布时间】:2020-10-31 22:41:48
【问题描述】:

如何使用序列化程序更新我的用户配置文件,当我更新我的用户配置文件时出现此错误:为序列化程序 accounts.serializers.AccountProfileSerializer 编写显式 .update() 方法,或在点源序列化程序字段上设置 read_only=True

class AccountProfileSerializer(serializers.ModelSerializer):
    gender = serializers.CharField(source='accountprofile.gender')
    phone = serializers.CharField(source='accountprofile.phone')
    location = serializers.CharField(source='accountprofile.location')
    birth_date = serializers.CharField(source='accountprofile.birth_date')
    biodata = serializers.CharField(source='accountprofile.biodata')

    class Meta:
        model = User
        fields = ('first_name', 'last_name', 'email', 'last_login', 'date_joined',
                  'gender', 'phone', 'location', 'birth_date', 'biodata')



class AccountProfileViewSet(APIView):
    permission_classes = [
        permissions.IsAuthenticated,
    ]

    def get(self, request, format=None):
        profile = User.objects.get(pk=self.request.user.pk)
        serializer = AccountProfileSerializer(profile, many=False)
        return Response(serializer.data)

    def put(self, request):
        profile = User.objects.get(pk=self.request.user.pk)
        serializer = AccountProfileSerializer(profile, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_200_OK)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

【问题讨论】:

  • 能否请您复制粘贴错误的完整回溯?
  • AssertionError at /api/accounts/profile/ .update() 方法默认不支持可写点源字段。为序列化器accounts.serializers.AccountProfileSerializer 编写显式.update() 方法,或在点源序列化器字段上设置read_only=True。这是完整的错误
  • create方法中弹出属于accountprofile的数据并将它们与不属于User模型的字段分开,然后将它们分别保存在create中。或者您可以编辑问题并在那里添加accountprofile Model,以便我可以帮助您全面实施。
  • 对不起我的意思是update方法

标签: django django-models django-rest-framework django-views django-serializer


【解决方案1】:

我通过重写序列化程序中的更新方法解决了这个问题

class AccountProfileSerializer(serializers.ModelSerializer):
    gender = serializers.CharField(source='accountprofile.gender')
    phone = serializers.CharField(source='accountprofile.phone')
    location = serializers.CharField(source='accountprofile.location')
    birth_date = serializers.CharField(source='accountprofile.birth_date')
    biodata = serializers.CharField(source='accountprofile.biodata')

    class Meta:
        model = User
        fields = ('first_name', 'last_name', 'email', 'gender',
                  'phone', 'location', 'birth_date', 'biodata')

    def update(self, instance, validated_data):
        profile_data = validated_data.pop('accountprofile')
        profile = instance.accountprofile

        # * User Info
        instance.first_name = validated_data.get(
            'first_name', instance.first_name)
        instance.last_name = validated_data.get(
            'last_name', instance.last_name)
        instance.email = validated_data.get(
            'email', instance.email)
        instance.save()

        # * AccountProfile Info
        profile.gender = profile_data.get(
            'gender', profile.gender)
        profile.phone = profile_data.get(
            'phone', profile.phone)
        profile.location = profile_data.get(
            'location', profile.location)
        profile.birth_date = profile_data.get(
            'birth_date', profile.birth_date)
        profile.biodata = profile_data.get(
            'biodata', profile.biodata)
        profile.save()

        return instance

【讨论】:

  • 我们只需要在其中一个字段未通过时检查“accountprofile”是否存在
猜你喜欢
  • 2016-12-28
  • 2021-07-09
  • 1970-01-01
  • 2019-11-16
  • 1970-01-01
  • 1970-01-01
  • 2017-06-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多