DRF TypeError __init __()只需1个参数(给定2个)

时间:2015-03-19 14:08:24

标签: python django python-2.7

当我尝试通过网络浏览器发出GET请求时出现此错误。
这是我的第一个DRF项目,我尝试制作api,以便能够在移动应用上使用它。我认为这个问题来自于我在views.py中所做的 这里有一些代码片段和Traceback:

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()

serializers.py

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


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

views.py

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

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
from rest_framework.generics import GenericAPIView

# Create your views here.

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

    def get(self, request):
        if request.GET.get("id"):
            queryset = Expediteur.objects.filter(id=request.GET["id"]).all()
        else:
            queryset = Expediteur.objects.all()
        serializer = self.serializer_class(queryset, many=True)
        return Response(serializer.data)

    def list(self,request):
          queryset  = self.Expediteur.objects.all()
          serializer = ExpediteurSerializer(self.get_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)

urls.py

from django.conf.urls import patterns, include, url
from rest_framework import routers
from django.contrib import admin
import api

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'CUP.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^api/', 'api.views.ExpeViewSet'),
)

回溯

Environment:


Request Method: GET
Request URL: http://0.0.0.0:8000/api/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',
 'api')
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)

Exception Type: TypeError at /api/expediteur/
Exception Value: __init__() takes exactly 1 argument (2 given)

1 个答案:

答案 0 :(得分:3)

你不能创建这样的网址。 ApiView是一个类,你需要使用as_view classmethod并直接引用该类,而不是字符串:

url(r'^api/', api.views.ExpeViewSet.as_view()),