gpt4 book ai didi

python - DRF TypeError __init__() 恰好接受 1 个参数(给定 2 个)

转载 作者:行者123 更新时间:2023-11-28 16:33:49 25 4
gpt4 key购买 nike

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

模型.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()

序列化器.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)

最佳答案

您不会像那样创建网址。 ApiView 是一个类,你需要使用 as_view 类方法并直接引用类,而不是作为字符串:

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

关于python - DRF TypeError __init__() 恰好接受 1 个参数(给定 2 个),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29146787/

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