我正在制作我的第一个DRF api,当我尝试使用GET请求访问我的数据时出现奇怪的TypeError,我收到此错误:
'type'对象不可迭代
下面是一些代码片段。
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
答案 0 :(得分:2)
如果您要列出所有expediteur
个实例,则应使用list
视图而不是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)
答案 1 :(得分:0)
就我而言,我不是从models.Model继承来的模型。
例如class Document:
...
应该是
class Document(models.Model):
...
答案 2 :(得分:-1)
我看到你使用QuerySet,但它应该是小写的。
试试这个:
from rest_framework.generics import GenericAPIView
class ExpeViewSet(GenericAPIView):
queryset = Expediteur.objects.all() # lowercase!
serializer_class = ExpediteurSerializer
def get(self, request):
serializer = self.serializer_class(self.get_queryset(), many=True)
return Response(serializer.data)