TypeError:__ init __()采用1个位置参数,但给出了2个

时间:2016-12-07 07:10:22

标签: python django

我正在使用Django 1.10开发一个简单的rest api 当我运行我的服务器并调用应用程序URL时,我收到一个错误:

  

TypeError:__init__()需要1个位置参数,但是给出了2个

GET / demo / HTTP / 1.1“500 64736

回溯

Environment:
Request Method: GET
Request URL: http://localhost:8000/demo/

Django Version: 1.10.4
Python Version: 3.5.2
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'mydemoapp',
 'rest_framework']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']

Traceback:

    File "/home/aqib/DjangoProject/mydemoenv/lib/python3.5/site-    packages/django/core/handlers/exception.py" in inner
    39. response = get_response(request)

    File "/home/aqib/DjangoProject/mydemoenv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response 
    187. response = self.process_exception_by_middleware(e, request)

    File "/home/aqib/DjangoProject/mydemoenv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
    185. response = wrapped_callback(request, *callback_args, **callback_kwargs)

    Exception Type: TypeError at /demo/
    Exception Value: __init__() takes 1 positional argument but 2 were given

models.py

from django.db import models

class ProfileModel(models.Model):
    name = models.CharField(max_length=30, blank=False, default='Your Name')
    address = models.CharField(max_length=100, blank=True)
    contact = models.IntegerField()

    def __str__(self):
        return '%s %s' % (self.name, self.address)

views.py

from django.shortcuts import render
from rest_framework import viewsets
from mydemoapp.models import ProfileModel
from .serializers import ProfileSerializer

class ProfileView(viewsets.ModelViewSet):
    profile = ProfileModel.objects.all()
    serializer_class = ProfileSerializer

serializers.py

from .models import ProfileModel
from rest_framework import serializers

class ProfileSerializer(serializers.ModelSerializer):

    class Meta:
        model = ProfileModel
        fields = ('name', 'address', 'contact')

urls.py(Application Url)

from django.conf.urls import url
from mydemoapp import views

urlpatterns = [
url(r'^$', views.ProfileView),
]

urls.py(项目网址)

from django.conf.urls import url, include
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^demo/', include('mydemoapp.urls')),
]

2 个答案:

答案 0 :(得分:3)

您使用的ViewSet网址错误。这是正确的方式

# project/urls.py
from django.conf.urls import url, include
from django.contrib import admin
from rest_framework import routers
from mydemoapp import views

router = routers.DefaultRouter()

router.register(r'demo', views.ProfileView)

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', include(router.urls)),
]

了解更多http://www.django-rest-framework.org/api-guide/routers/

回答评论

  

现在我得到了AssertionError:未指定base_name参数,并且无法自动确定视图集中的名称,因为它没有.queryset属性。

您的观点也不正确。它应该是queryset而不是profile

class ProfileView(viewsets.ModelViewSet):
    queryset = ProfileModel.objects.all()  # <-- here 
    serializer_class = ProfileSerializer

答案 1 :(得分:2)

我经常犯这种非常愚蠢的错误。这是由于urls.py(Application)。 永远记得调用 { "presets": [ "react-app" ], "plugins": [ "external-helpers" ] } 方法

错误

urls.py

.as_view()

正确

urls.py

from django.conf.urls import url
from mydemoapp import views

urlpatterns = [
url(r'^$', views.ProfileView),
]