不完整的json结构 - 没有计数/下一个/上一个/结果字段

时间:2018-03-08 17:57:33

标签: json django django-rest-framework

很多次之前,我试图将数据从我的Django后端发送到我的Ionic移动应用程序。

但是,这一次,由于某些原因,.jsons im解析结果不完整。

一个完整的.json:

{"count":1,"next":null,"previous":null,"results":[{"codigo":"qwe","area":"ewq","especies":[],"id":1}]}

我的不完整.json:

[{"nome_ficheiro":"1520529086252.jpg","id":26,"especie":"Ameijoa Branca","zona":"L6","data":"09/06/2018"}]

IONIC正在努力识别我正在解析的.json,这是有道理的,因为没有"结果"字段。

以下是我的django代码的相关摘要:

Views.py(两个视图都在做同样的事情!这只是我尝试不同的方法!)

class resultUploadViewSet(viewsets.ViewSet):
    def list(self, request, nome_ficheiro):
        queryset    = labelResult.objects.all()
        nome        = nome_ficheiro
        answer      = queryset.filter(nome_ficheiro=nome)
        serializer  = resultSerializer(answer, many=True)
        return Response(serializer.data)

class resultUploadView(APIView):
    serializer_class = resultSerializer

    def get(self, request, nome_ficheiro):
        queryset    = labelResult.objects.all()
        nome        = nome_ficheiro
        answer      = queryset.filter(nome_ficheiro=nome)
        serializer = self.serializer_class(answer, many=True)
        return Response(serializer.data)

Models.py

class labelResult(models.Model):
    nome_ficheiro   = models.CharField(max_length=120)
    especie         = models.CharField(max_length=120)
    zona            = models.CharField(max_length=120)
    data            = models.CharField(max_length=120)

Urls.py

urlpatterns = [
url(r'results/(?P<nome_ficheiro>.+)/$', resultUploadViewSet.as_view({'get': 'list'})),
url(r'results1/(?P<nome_ficheiro>.+)/$', resultUploadView.as_view())]

Serializers.py

class resultSerializer(serializers.ModelSerializer):
    class Meta:
        model =  labelResult
    fields = ('nome_ficheiro','id','especie','zona', 'data')

知道为什么我的.jsons出来不完整了吗?

1 个答案:

答案 0 :(得分:1)

您应该使用ListAPIView,以便应用分页。 http://www.django-rest-framework.org/api-guide/generic-views/#listapiview

更多有关分页的信息,请点击此处: http://www.django-rest-framework.org/api-guide/pagination/