TypeError: __init__() takes 1 positional argument but 2 were given
13,449
Solution 1
You are using ViewSet
urls wrong. This is right way
# 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)),
]
Read more http://www.django-rest-framework.org/api-guide/routers/
Answering comment
Now i get AssertionError: base_name argument not specified, and could not automatically determine the name from the viewset, as it does not have a .queryset attribute.
Your view is incorrect as well. It should spicify queryset
not profile
class ProfileView(viewsets.ModelViewSet):
queryset = ProfileModel.objects.all() # <-- here
serializer_class = ProfileSerializer
Solution 2
This very silly mistake I do too often. This is because of the urls.py(Application).
Always remember to call the .as_view()
method
Wrong
urls.py
from django.conf.urls import url
from mydemoapp import views
urlpatterns = [
url(r'^$', views.ProfileView),
]
Correct
urls.py
from django.conf.urls import url
from mydemoapp import views
urlpatterns = [
url(r'^$', views.ProfileView.as_view()),
]
Related videos on Youtube
Author by
Mohd Aqib
Updated on June 04, 2022Comments
-
Mohd Aqib almost 2 years
I am develop a simple rest api using Django 1.10 When I run my server and call app url I get an error:
TypeError:
__init__()
takes 1 positional argument but 2 were givenGET /demo/ HTTP/1.1" 500 64736
Traceback
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 (project url)
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')), ]
-
OneCricketeer over 7 yearsAny reason for using Django? Seems excessive for just a simple REST API
-
Goodies over 7 yearsCan we have a more complete traceback/example? When is this being executed? What file? Upon what call?
-
Goodies over 7 years@cricket_007 Not relevant, but not untrue.
-
Goodies over 7 years@MohdAqib That's hardly more information. is the exception being raised or handled? Have you ran through with a debugger at all?
-
metmirr over 7 years@MohdAqib
url(r'^demo/$', include('mydemoapp.urls'))
try $ -
Mohd Aqib over 7 years@metmirr I tried but its not working
-
metmirr over 7 yearsyou have to show full traceback
-
Mohd Aqib over 7 years@metmirr Now you can see the full traceback.
-
-
trevdev about 4 years3 Years later in Django 3.0 this bailed me out. I'm having troubles understanding why
.as_view()
is required. In my situation I'm using django rest, and I want an endpoint to run an internal operation, rather than just serve or modify data. The ModelViewSet does not require .as_view() to work, but APIView does. I'm assuming this is just abstraction setting a weird precedent? -
Armen Sanoyan over 3 years@trevdev Because Django’s URL resolver expects to send the request and associated arguments to a callable function, not a class, class-based views have an as_view() class method which returns a function that can be called when a request arrives for a URL matching the associated pattern. For more details look here docs.djangoproject.com/en/3.1/topics/class-based-views/intro/…