Django - AttributeError:'Fichas'对象没有属性'update'

时间:2014-10-13 10:24:02

标签: django django-rest-framework

我有一个序列化程序,用于验证某些字段是否为空。如果一个字段是空的,那么一切都很完美,但是如果所有的字段都是正确的,我得到了和AttributeError:' Fichas'对象没有属性'更新'。

这是视图和序列化器:

views.py

class PublicarFichaViewSet(APIView):
    permission_classes = (IsAuthenticated, GroupPermission)
    def get_object(self, pk):
        try:
            return Fichas.objects.get(pk=pk)
        except:
            raise Http404

    def get(self, request, pk, format=None):
        ficha = self.get_object(pk)
        serializer = PublicarFichaSerializer(ficha)
        return Response(serializer.data)

    def put(self, request, pk, format=None):
        ficha = self.get_object(pk)
        serializer = PublicarFichaSerializer(ficha, data=request.DATA)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

serializer.py

class PublicarFichaSerializer(serializers.Serializer):
    Centro_responsable = serializers.CharField(required=True, max_length=64)
    Autor_de_la_ficha = serializers.CharField(required=True, max_length=64)
    Correo_electronico_del_autor_de_la_ficha = serializers.EmailField(required=True)
    Autor_del_caso_de_estudio = serializers.CharField(required=True, max_length=64)
    Correo_electronico_del_autor_del_caso_de_estudio = serializers.EmailField(required=True)
    Tutor = serializers.CharField(required=True, max_length=64)
    Correo_electronico_del_tutor = serializers.EmailField(required=True)
    Fecha_de_tutela = serializers.DateField(required=True)
    Titulo = serializers.CharField(required=True, max_length=64)
    Tipo_de_objeto_u_obra_por_su_funcion = serializers.CharField(required=True, max_length=64)
    Plano_de_situacion = serializers.CharField(required=True, max_length=64)
    Fecha_de_validez_legal = serializers.DateField(required=True)
    Ficha_publicada = serializers.BooleanField()

    def validate_Ficha_publicada(self, attrs, source):
        if not attrs['Ficha_publicada']:
            raise serializers.ValidationError("Ficha no publicada")
        return attrs

编辑:追溯

Environment:


Request Method: POST
Request URL: http://localhost:8000/publicarficha/1/

Django Version: 1.6.5
Python Version: 2.7.6
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'corsheaders',
 'rest_framework',
 'phi_catalogo',
 'rest_framework.authtoken',
 'django_evolution',
 'django.contrib.admin',
 'multiselectfield',
 'django_extensions',
 'pytz')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'corsheaders.middleware.CorsMiddleware')


Traceback:
File "/Users/jian/phi/env/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  112.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/jian/phi/env/lib/python2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
  57.         return view_func(*args, **kwargs)
File "/Users/jian/phi/env/lib/python2.7/site-packages/django/views/generic/base.py" in view
  69.             return self.dispatch(request, *args, **kwargs)
File "/Users/jian/phi/env/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
  403.             response = self.handle_exception(exc)
File "/Users/jian/phi/env/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
  400.             response = handler(request, *args, **kwargs)
File "/Users/jian/phi/backend/phi_catalogo/views.py" in put
  148.         if serializer.is_valid():
File "/Users/jian/phi/env/lib/python2.7/site-packages/rest_framework/serializers.py" in is_valid
  550.         return not self.errors
File "/Users/jian/phi/env/lib/python2.7/site-packages/rest_framework/serializers.py" in errors
  542.                 ret = self.from_native(data, files)
File "/Users/jian/phi/env/lib/python2.7/site-packages/rest_framework/serializers.py" in from_native
  375.             return self.restore_object(attrs, instance=getattr(self, 'object', None))
File "/Users/jian/phi/env/lib/python2.7/site-packages/rest_framework/serializers.py" in restore_object
  335.             instance.update(attrs)

Exception Type: AttributeError at /publicarficha/1/
Exception Value: 'Fichas' object has no attribute 'update'

1 个答案:

答案 0 :(得分:2)

尝试使用ModelSerializer而不是Serializer作为超类。