gpt4 book ai didi

python - DRF TypeError 'type' 对象不可迭代

转载 作者:太空狗 更新时间:2023-10-30 01:28:42 25 4
gpt4 key购买 nike

我正在制作我的第一个 DRF api,当我尝试使用 GET 请求访问我的数据时,我得到了一个奇怪的 TypeError,我得到了这个错误:

'type' object is not iterable

下面是一些代码片段。

模型.py

from django.db import models

# Create your models here.

class Expediteur(models.Model):
nom = models.CharField(max_length=50)
prenom = models.CharField(max_length=100)
adresse = models.CharField(max_length=200)
tel = models.IntegerField()

views.py

from django.shortcuts import render, get_object_or_404
from rest_framework import viewsets
from rest_framework.views import APIView
from rest_framework.response import Response
from polls.serializers import ExpediteurSerializer, DestinataireSerializer, LettrePrioSerializer, TypeLettreRecoSerializer, LettreRecoSerializer, TimbrePrioSerializer, TimbreRecoSerializer
from polls.models import Expediteur, Destinataire, LettrePrio, TypeLettreReco, LettreReco, TimbrePrio, TimbreReco
from rest_framework import status, HTTP_HEADER_ENCODING

import json
import datetime

from django.http import HttpResponseRedirect, HttpResponse, Http404
from django.core.urlresolvers import reverse
from django.contrib.auth.decorators import user_passes_test
from django.contrib.auth import login
from django.core.exceptions import ObjectDoesNotExist
from django.views.decorators.csrf import csrf_exempt
from django.utils.timezone import get_current_timezone
from django.db.models.query import QuerySet

# Create your views here.

class ExpeViewSet(APIView):
QuerySet = Expediteur.objects.all()
serializer_class = ExpediteurSerializer

def get(self, request):
serializer = ExpediteurSerializer(QuerySet, many=True)
return Response(serializer.data)

def post(self, request):
serializer = self.serializer_class(data=request.DATA)
return Response(status=status.HTTP_201_CREATED)

序列化器.py

from rest_framework import serializers
from polls.models import Expediteur, Destinataire, LettrePrio, TypeLettreReco, LettreReco, TimbrePrio, TimbreReco


class ExpediteurSerializer(serializers.ModelSerializer):
class Meta:
model = Expediteur

我还留下了追溯以获取更多信息:

Environment:


Request Method: GET
Request URL: http://0.0.0.0:8000/polls/expediteur/

Django Version: 1.7.1
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'polls')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "/Library/Python/2.7/site-packages/Django-1.7.1-py2.7.egg/django/core/handlers/base.py" in get_response
111. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Library/Python/2.7/site-packages/Django-1.7.1-py2.7.egg/django/views/decorators/csrf.py" in wrapped_view
57. return view_func(*args, **kwargs)
File "/Library/Python/2.7/site-packages/Django-1.7.1-py2.7.egg/django/views/generic/base.py" in view
69. return self.dispatch(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/rest_framework/views.py" in dispatch
452. response = self.handle_exception(exc)
File "/Library/Python/2.7/site-packages/rest_framework/views.py" in dispatch
449. response = handler(request, *args, **kwargs)
File "/Users/mehdi/rendu/ProjectWeek/CUP/polls/views.py" in get
29. return Response(serializer.data)
File "/Library/Python/2.7/site-packages/rest_framework/serializers.py" in data
618. ret = super(ListSerializer, self).data
File "/Library/Python/2.7/site-packages/rest_framework/serializers.py" in data
213. self._data = self.to_representation(self.instance)
File "/Library/Python/2.7/site-packages/rest_framework/serializers.py" in to_representation
568. self.child.to_representation(item) for item in iterable

Exception Type: TypeError at /polls/expediteur/
Exception Value: 'type' object is not iterable

最佳答案

如果您想列出所有expediteur 实例,您应该使用list View 而不是get。请记住,get 用于检索一个实例。

此外,在您的导入中,您有 from django.db.models.query import QuerySet 然后当您尝试 ExpediteurSerializer(QuerySet, many=True) 时,您正在尝试序列化之前导入的 QuerySet 类型。要获得定义的 QuerySet,您需要使用 self。 但我建议只使用 queryset

class ExpeViewSet(GenericAPIView):
queryset = Expediteur.objects.all()
serializer_class = ExpediteurSerializer


def list(self,request):
queryset = self.Expediteur.objects.all()
serializer = ExpediteurSerializer(self.get_queryset(), many=True)
return Response(serializer.data)

关于python - DRF TypeError 'type' 对象不可迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29134642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com