Django REST Framework上的动态更新实例

时间:2016-08-31 05:25:48

标签: python django django-rest-framework

您好我正在尝试使用自定义方法更新Django REST Framework动态更新,如下所示:

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)

但是我收到了这个错误:

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

2 个答案:

答案 0 :(得分:2)

改为使用setattr(instance, "name", value)

setattr(instance, "name", self.validated_data.get('name', instance.name))

确实无法通过“项目分配”更改django对象属性。

答案 1 :(得分:2)

这对我有用。我希望它也能为您服务。

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