Django REST - PATCH for replacing nested object
<p>I'm trying to replace one tested object another using PATCH request. For My experiments I use Postman. I send PATCH request with parameter user_status and value:</p>

<blockquote>
  <p>{"id": 1, "status_type": {"id": 2, "name": "new_status_type_name"},
  "name": "new_status_name"}</p>
</blockquote>

<p>I wrote update method for updating my ResultSerializer, but it doesn't work. Now I'm debuging it and I see that variable validated_data doesn't contain my new user_status. user_status is an empty OrderedDict:</p>

<pre><code>ipdb&gt; validated_data['user_status']
OrderedDict()
</code></pre>

<p>I checked my request and I see that user_status is a list with one element - string.</p>

<pre><code>ipdb&gt; self.context['request'].data
&lt;QueryDict: {'user_status': ['{"id": 1, "status_type": {"id": 2, "name": "new_status_type_name"}, "name": "new_status_name"}']}&gt;
</code></pre>

<p>How can I replace one another nested objects? Thank you for your help.</p>

<p>I have next Models:</p>

<pre><code>class UserStatus(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(unique=True, max_length=255)
    status_type = models.ForeignKey('StatusType', blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'user_status'

    def __str__(self):
        return self.name


class StatusType(models.Model):
    id = models.SmallIntegerField(primary_key=True)
    name = models.CharField(unique=True, max_length=256)

    class Meta:
        managed = False
        db_table = 'status_type'

    def __unicode__(self):
        return self.name

class Result(models.Model):
    id = models.BigIntegerField(primary_key=True)
    user_status = models.ForeignKey('UserStatus', blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'result'
</code></pre>

<p>Serializers:</p>

<pre><code>class UserStatusSerializer(serializers.ModelSerializer):
    id = serializers.IntegerField()
    name = serializers.CharField()
    status_type = StatusTypeSerializer()

    class Meta:
        model = app.models.UserStatus


class StatusTypeSerializer(serializers.ModelSerializer):
    id = serializers.IntegerField()
    name = serializers.CharField()

    class Meta:
        model = app.models.StatusType


class ResultSerializer(serializers.ModelSerializer):
    user_status = UserStatusSerializer(many=False)

    def update(self, instance, validated_data, *args, **kwargs):
        import ipdb; ipdb.set_trace()
        instance.user_status = validated_data.get('user_status', instance.user_status)
        instance.save()
        return instance

    class Meta:
        model = app.models.Result
</code></pre>

<p>Views:</p>

<pre><code>class StatusTypeViewSet(viewsets.ModelViewSet):
    queryset = app.models.StatusType.objects.all()
    serializer_class = app.serializers.StatusTypeSerializer


class UserStatusViewSet(viewsets.ModelViewSet):
    queryset = app.models.UserStatus.objects.all()
    serializer_class = app.serializers.UserStatusSerializer


class ResultViewSet(viewsets.ModelViewSet):
    queryset = app.models.Result.objects.all()
    serializer_class = app.serializers.ResultSerializer
</code></pre>