POST不工作Django Rest Framework

时间:2016-12-01 23:29:52

标签: django django-rest-framework

我在google中找到邮件返回,而不是查找。 什么是我的代码不发布值正确? 我需要帮助解决方案正确。 作为基于使用形式的通用视图? 我desenv restAPI,我不理解我的代码中的问题,我运行并返回: 我检索消息,流程。 enter image description here

enter image description here

views.py:

import sys
import time
import datetime
import os


class Application():

    def loop_one(self):

       filename = 'Test.csv'
       mycsv = open(filename, 'r')
       mycsv.seek(0, os.SEEK_END)

       while 1:
            time.sleep(1)
            where = mycsv.tell()
            line = mycsv.readline()

       if not line:
            mycsv.seek(where)

       else:
           arr_line = line.split(',')
           var1 = arr_line[5]
           mydate = datetime.datetime.now()
           print var1, mydate.strftime("%H:%M:%S:%f")
           return var1

    def loop_two(self):

       filename2 = 'Test2.csv'
       mycsv2 = open(filename2, 'r')
       mycsv2.seek(0, os.SEEK_END)

       while 1:
            time2.sleep(1)
            where2 = mycsv2.tell()
            line2 = mycsv2.readline()

       if not line2:
            mycsv2.seek(where2)

       else:
           arr_line2 = line2.split(',')
           var2 = arr_line2[5]
           mydate2 = datetime.datetime.now()
           print var2, mydate.strftime("%H:%M:%S:%f")
           return var2

s = Application()
var1 = s.loop_one()
var2 = s.loop_two()

serializers.py

from snippets.models import Equipamento, Colaborador
from snippets.serializers import EquipamentoSerializer, ColaboradorSerializer
from rest_framework import mixins
from rest_framework import generics

class EquipamentoList(generics.ListCreateAPIView):
    serializer_class = EquipamentoSerializer

    def get_queryset(self):
        queryset = Equipamento.objects.all()
        id = self.request.query_params.get('id', None)
        if id is not None:
            queryset = queryset.filter(id=id)
        return queryset


# class ColaboradorList(generics.CreateAPIView):
#     queryset = Colaborador.objects.all()
#     serializer_class = ColaboradorSerializer

#     def get_queryset(self):
#         queryset = Colaborador.objects.all()
#         id = self.request.query_params.get('id', None)
#         if id is not None:
#             queryset = queryset.filter(pk=pk)
#         return queryset

#     def create(self, request, pk):
#         queryset = Colaborador.objects.all()
#         return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)



# class ColaboradorDetail(generics.RetrieveUpdateDestroyAPIView):
#     queryset = Colaborador.objects.all()
#     serializer_class = ColaboradorSerializer

class ColaboradorList(mixins.ListModelMixin,
                  mixins.CreateModelMixin,
                  generics.GenericAPIView):
    queryset = Colaborador.objects.all()
    serializer_class = ColaboradorSerializer

    def get(self, request, *args, **kwargs):
        return self.list(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        return self.create(request, *args, **kwargs)

class ColaboradorDetail(mixins.RetrieveModelMixin,
                    mixins.UpdateModelMixin,
                    mixins.DestroyModelMixin,
                    generics.GenericAPIView):
    queryset = Colaborador.objects.all()
    serializer_class = ColaboradorSerializer

    def get(self, request, *args, **kwargs):
        return self.retrieve(request, *args, **kwargs)

    def put(self, request, *args, **kwargs):
        return self.update(request, *args, **kwargs)

    def delete(self, request, *args, **kwargs):
        return self.destroy(request, *args, **kwargs)

urls.py

from rest_framework import serializers
from rest_framework.validators import UniqueValidator
from snippets.models import Equipamento, Colaborador, Propriedade, MotivoParada, Apontamento

class EquipamentoSerializer(serializers.Serializer):
    id = serializers.IntegerField(read_only=True)
    cod_equip = serializers.IntegerField(validators=[UniqueValidator(queryset=Equipamento.objects.all())])
    desc_equip = serializers.CharField(allow_blank=True, max_length=15, required=False)

    def restore_object(self, attrs, instance=None):
        """
        Create or update a new snippet instance, given a dictionary
        of deserialized field values.

        Note that if we don't define this method, then deserializing
        data will simply return a dictionary of items.
        """
        if instance:
            # Update existing instance
            instance.id = attrs.get('id', instance.id)
            instance.cod_equip = attrs.get('cod_equip', instance.cod_equip)
            instance.des_equip = attrs.get('desc_equip', instance.desc_equip)
            return instance
        # Create new instance
        return Equipamento(**attrs)

class ColaboradorSerializer(serializers.Serializer):
    id = serializers.IntegerField(read_only=True)
    cod_colab = serializers.IntegerField(validators=[UniqueValidator(queryset=Colaborador.objects.all())])
    nome_colab = serializers.CharField(max_length=30)

    def restore_object(self, attrs, instance=None):
        """
        Create or update a new snippet instance, given a dictionary
        of deserialized field values.

        Note that if we don't define this method, then deserializing
        data will simply return a dictionary of items.
        """
        if instance:
            # Update existing instance
            instance.id = attrs.get('id', instance.id)
            instance.cod_colab = attrs.get('cod_colab', instance.cod_colab)
            instance.nome_colab = attrs.get('nome_colab', instance.nome_colab)
            return instance
        # Create new instance
        return Colaborador(**attrs)

class ApontamentoSerializer(serializers.Serializer):
    id = serializers.IntegerField(read_only=True)
    criado = serializers.DateTimeField(read_only=True)
    apont_inicio = serializers.TimeField()
    apont_fim = serializers.TimeField()
    duracao = serializers.TimeField()
    equipamento = serializers.PrimaryKeyRelatedField(queryset=Equipamento.objects.all())
    colaborador = serializers.PrimaryKeyRelatedField(queryset=Colaborador.objects.all())
    propriedade = serializers.PrimaryKeyRelatedField(queryset=Propriedade.objects.all())
    m_parada = serializers.PrimaryKeyRelatedField(queryset=MotivoParada.objects.all())

    def restore_object(self, attrs, instance=None):
        """
        Create or update a new snippet instance, given a dictionary
        of deserialized field values.

        Note that if we don't define this method, then deserializing
        data will simply return a dictionary of items.
        """
        if instance:
            # Update existing instance
            instance.id = attrs.get('id', instance.id)
            instance.criado = attrs.get('criado', instance.criado)
            instance.apont_inicio = attrs.get('apont_inicio', instance.apont_inicio)
            instance.apont_fim = attrs.get('apont_fim', instance.apont_fim)
            instance.duracao = attrs.get('duracao', instance.duracao)
            instance.equipamento = attrs.get('equipamento', instance.equipamento)
            instance.colaborador = attrs.get('colaborador', instance.colaborador)
            instance.propriedade = attrs.get('propriedade', instance.propriedade)
            instance.m_parada = attrs.get('m_parada', instance.m_parada)
            return instance
        # Create new instance
        return Apontamento(**attrs)

class PropriedadeSerializer(serializers.Serializer):
    id = serializers.IntegerField(read_only=True)
    cod_prop = serializers.IntegerField(validators=[UniqueValidator(queryset=Propriedade.objects.all())])
    desc_prop = serializers.CharField(max_length=30)

    def restore_object(self, attrs, instance=None):
        """
        Create or update a new snippet instance, given a dictionary
        of deserialized field values.

        Note that if we don't define this method, then deserializing
        data will simply return a dictionary of items.
        """
        if instance:
            # Update existing instance
            instance.id = attrs.get('id', instance.id)
            instance.cod_prop = attrs.get('cod_prop', instance.cod_prop)
            instance.des_prop = attrs.get('desc_prop', instance.desc_prop)
            return instance
        # Create new instance
        return Propriedade(**attrs)

class MotivoParadaSerializer(serializers.Serializer):
    id = serializers.IntegerField(read_only=True)
    cod_mparada = serializers.IntegerField(validators=[UniqueValidator(queryset=MotivoParada.objects.all())])
    desc_mparada = serializers.CharField(max_length=30)

    def restore_object(self, attrs, instance=None):
        """
        Create or update a new snippet instance, given a dictionary
        of deserialized field values.

        Note that if we don't define this method, then deserializing
        data will simply return a dictionary of items.
        """
        if instance:
            # Update existing instance
            instance.id = attrs.get('id', instance.id)
            instance.cod_mparada = attrs.get('cod_mparada', instance.cod_mparada)
            instance.des_mparada = attrs.get('desc_mparada', instance.desc_mparada)
            return instance
        # Create new instance
        return MotivoParada(**attrs)

帮助。 什么是我的代码不发布值? 我需要帮助才能解决问题。

1 个答案:

答案 0 :(得分:0)

错误消息非常简单。您的序列化程序使用restore_object方法,该方法在Rest Framework 3中已弃用。要么将Django Rest Framework版本降级到2.x,要么(推荐)重写代码以使其与Rest Framework的最新版本兼容。