我正在制作我的第一个DRF api,当我试图使用GET请求访问我的数据时,我得到了一个奇怪的TypeError,我得到了这个错误。
'类型'对象不是可迭代的
下面是一些代码片段。
models.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)
serializers.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
我还留下了Traceback以获得更多信息。
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