Reputation: 7384
Hi I'm trying to update dynamically using custom method update Django REST Framework like this:
serializers.py
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
views.py
@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)
However I get this error:
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
Upvotes: 2
Views: 3647
Reputation: 616
This worked for me. I hope it can as well work for you.
def update(self, instance, validated_data):
# instance.username = validated_data.get('username', instance.username)
# instance.email = validated_data.get('email', instance.email)
# print(validated_data)
# print(validated_data.keys())
for (key, value) in validated_data.items():
setattr(instance, key, value)
instance.save()
return instance
Upvotes: 3
Reputation: 7596
Use setattr(instance, "name", value)
instead:
setattr(instance, "name", self.validated_data.get('name', instance.name))
It's not possible to change django object attributes by "item assignments" indeed.
Upvotes: 2