简体   繁体   English

Django Rest框架和python3.5 OrderedDict在迭代过程中发生了变异

[英]Django rest framework and python3.5 OrderedDict mutated during iteration

I use Django rest framework and python3.5. 我使用Django rest框架和python3.5。 Earlier I had another version of python and everything was going right. 早些时候,我有另一个版本的python,一切运行正常。 When I want to get some information from server with URL for example like: 当我想从服务器获取带有URL的信息时,例如:

" http://127.0.0.1:8000/api/companies " http://127.0.0.1:8000/api/companies

I'm getting error: 我收到错误:

"OrderedDict mutated during iteration" “ OrderedDict在迭代过程中发生了变异”

.

In views.py I have: 在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

What should I do to make it working? 我应该怎么做才能使其正常工作? Where is something mutating the dict? 哪里有什么改变该命令?

I don't know why using ListCreateApiView is mutating dict, but I changed class into function like : 我不知道为什么使用ListCreateApiView突变字典,但我将类更改为类似的功能:

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

and now it's working... 现在正在运作...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM