Django休息框架和python3.5 OrderedDict在迭代期间发生了变异

时间:2016-02-24 20:47:54

标签: python django django-rest-framework

我使用Django rest框架和python3.5。之前我有另一个版本的python,一切都很顺利。当我想从带有URL的服务器获取一些信息时,例如:

  

http://127.0.0.1:8000/api/companies

我收到错误:

  

“OrderedDict在迭代期间发生变异”

在views.py中我有:

from django.shortcuts import render
from companies.models import Companies
from companies.serializers import CompaniesSerializer
from rest_framework import generics
from rest_framework.response import Response
from rest_framework.renderers import JSONRenderer
from rest_framework import status


class CompaniesList(generics.ListCreateAPIView):
    queryset = Companies.objects.all()
    serializer_class = CompaniesSerializer


class CompaniesDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = Companies.objects.all()
    serializer_class = CompaniesSerializer

我该怎么做才能让它发挥作用?哪些东西会改变字典?

1 个答案:

答案 0 :(得分:0)

我不知道为什么使用ListCreateApiView改变dict,但是我将类更改为如下函数:

@api_view(['GET'])
    def CompaniesList(request):
        if request.method == 'GET':
            companies = Companies.objects.all()
            serializer = CompaniesSerializer(companies, many=True)
            return Response(serializer.data)

现在它正在运作......