Dynamic update instance on Django REST Framework
<p>Hi I'm trying to update dynamically using custom method update Django REST Framework like this:</p>

<p><strong>serializers.py</strong></p>

<pre><code>class ArtistSerializer(serializers.Serializer):
    id = serializers.IntegerField(required=False)
    name = serializers.CharField(max_length=100, required=True)

    def create(self, validated_data):
        Personnels.objects.create(**self.validated_data)

    def update(self, instance, validated_data):
        # for k,v in self.validated_data.iteritems():
            # instance['k'] = self.validated_data.get(k, instance[k])
        # instance.name = self.validated_data.get('name', instance.name)
        instance['name'] = self.validated_data.get('name', instance.name)
        instance.save()
        return instance
</code></pre>

<p><strong>views.py</strong></p>

<pre><code>@api_view(['PUT'])
def artist_serialization_specific(request, artist_id):
    if request.method == "PUT": # Example of updating an object via manual serialization
        # return HttpResponse(artist_id)
        personnel = Personnels.objects.get(id=artist_id)
        x = ArtistSerializer(personnel, data={"name":"John Doe"})
        if x.is_valid():
            x.update(personnel, x.validated_data)
        else:
            print x.errors
        return Response(x.data, status=status.HTTP_200_OK)
</code></pre>

<p>However I get this error:</p>

<pre><code>Internal Server Error: /forms/artists/serialization/3/
Traceback (most recent call last):
  File "/Users/deanarmada/.virtualenvs/django-cassandra/lib/python2.7/site-packages/django/core/handlers/exception.py", line 39, in inner
    response = get_response(request)
  File "/Users/deanarmada/.virtualenvs/django-cassandra/lib/python2.7/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
    response = self._get_response(request)
  File "/Users/deanarmada/.virtualenvs/django-cassandra/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/deanarmada/.virtualenvs/django-cassandra/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/deanarmada/.virtualenvs/django-cassandra/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "/Users/deanarmada/.virtualenvs/django-cassandra/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/deanarmada/.virtualenvs/django-cassandra/lib/python2.7/site-packages/rest_framework/views.py", line 474, in dispatch
    response = self.handle_exception(exc)
  File "/Users/deanarmada/.virtualenvs/django-cassandra/lib/python2.7/site-packages/rest_framework/views.py", line 434, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/Users/deanarmada/.virtualenvs/django-cassandra/lib/python2.7/site-packages/rest_framework/views.py", line 471, in dispatch
    response = handler(request, *args, **kwargs)
  File "/Users/deanarmada/.virtualenvs/django-cassandra/lib/python2.7/site-packages/rest_framework/decorators.py", line 52, in handler
    return func(*args, **kwargs)
  File "/Users/deanarmada/Desktop/projects/python-projects/django/django-cassandra/tutorial/example/views.py", line 85, in artist_serialization_specific
    x.update(personnel, x.validated_data)
  File "/Users/deanarmada/Desktop/projects/python-projects/django/django-cassandra/tutorial/example/serializers.py", line 21, in update
    instance['name'] = self.validated_data.get('name', instance.name)
TypeError: 'Personnels' object does not support item assignment
</code></pre>

<p>Use <code>setattr(instance, "name", value)</code> instead:</p>

<pre><code>setattr(instance, "name", self.validated_data.get('name', instance.name))
</code></pre>

<p>It's not possible to change django object attributes by "item assignments" indeed.</p>

python django django-rest-framework