此字段必须是 Django Rest Framework 中 OnetoOneField 邮递员中的唯一错误

This field must be unique error in postman in OnetoOneField in Django Rest Framework

我正在尝试更新客户资料,同时使用 嵌套序列化更新主要的 Customuser first_name 和 last_name 字段。 但我越来越客户字段必须是唯一错误。

我把照片贴在这里了。

我的模特:

class CustomUser(AbstractUser):
    # username = None
    first_name = models.CharField(max_length=255, verbose_name="First name")
    last_name = models.CharField(max_length=255, verbose_name="Last name")
    email = models.EmailField(unique=True)
    
    is_seller = models.BooleanField(default=False)
    is_customer = models.BooleanField(default=False)

    USERNAME_FIELD = "email"
    REQUIRED_FIELDS = ["first_name", "last_name"]

    objects = CustomUserManager()

    def __str__(self):
        return self.email


class Customer(models.Model):
    customer = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, blank=True, null=True)
    full_name = models.CharField(max_length=100, blank=True)
    phone_num = models.CharField(max_length=50, blank=True)
    #dob = models.CharField(max_length=255,blank=True,null=True)
    region = models.CharField(max_length=255, blank=True,null=True)
    city = models.CharField(max_length=255, blank=True, null=True)
    area = models.CharField(max_length=255,blank=True,null=True)
    address = models.CharField(max_length=255, blank=True, null=True)


    def __str__(self):
        return self.customer.email

我的序列化程序:

class CustomerProfileSerializer(serializers.ModelSerializer):
    class Meta:
        model = Customer
        fields = '__all__'
        # depth = 1


class CustomerUpdateSerializer(serializers.ModelSerializer):
    customer = CustomerProfileSerializer()
    class Meta:
        model = User
        fields = ('id', "first_name", "last_name",'customer')

    def update(self,request, instance, validated_data):
        user = self.request.user
        instance.user.first_name=user.get('first_name')
        instance.user.last_name = user.get('last_name')
        instance.user.save()
        customer_data = validated_data.pop('customer',None)
        if customer_data is not None:
            instance.customer.region = customer_data['region']
            instance.customer.city = customer_data['city']
            instance.customer.area = customer_data['area']
            instance.customer.address = customer_data['address']
            instance.customer.save()
        return super().update(instance,validated_data)

我的看法:

class CustomerUpdateView(UpdateAPIView):
    permission_classes = [IsAuthenticated]
    queryset = User.objects.all()
    serializer_class = CustomerUpdateSerializer

url 在 postman put 请求中可见。我已经在邮递员中发送了原始 json 数据,但它抛出了这个错误。如何从用户模型更新客户中的这 4 个字段和 2 个字段(first_name 和 last_name)?

我对序列化程序进行了一些更改后,我的代码工作正常。

这是工作代码:

class CustomerUpdateSerializer(serializers.ModelSerializer):
    customer = CustomerProfileSerializer(many=False)
    class Meta:
        model = User
        fields = ('id', "first_name", "last_name",'customer')
        depth = 1

    def update(self, instance, validated_data):
        user = self.context['request'].user
        user.first_name = validated_data.get('first_name')
        user.last_name = validated_data.get('last_name')
        user.save()
        customer_data = validated_data.pop('customer',None)
        if customer_data is not None:
            instance.customer.region = customer_data['region']
            instance.customer.city = customer_data['city']
            instance.customer.area = customer_data['area']
            instance.customer.address = customer_data['address']
            instance.customer.save()
        return super().update(instance,validated_data)

这是因为我正在使用用户实例和客户模型的相同实例。现在,我分别获取字段 first_name 和 last_name,并使用实例为客户 only.It 工作。