user11149657
user11149657

Reputation:

Django Rest API Type error for positional argument

Django fires me this error and i am not getting why..

TypeError at /api/v1/article

__init__() takes 1 positional argument but 2 were given

Request Method:     GET
Request URL:    http://127.0.0.1:8000/api/v1/article
Django Version:     2.2.2
Exception Type:     TypeError
Exception Value:    

__init__() takes 1 positional argument but 2 were given

This is my serializer class:

class ArticleSerializer(serializers.ModelSerializer):
    class Meta:
        model =  Article
        fields = ['id', 'title', 'body', 'category']

these are my models:

from django.db import models
from django.contrib.auth.models import User

class Author(models.Model):
    name = models.ForeignKey(User, on_delete=models.CASCADE)
    detail = models.TextField()

class Category(models.Model):
    name = models.CharField(max_length=100)

class Article(models.Model):
    author = models.ForeignKey(Author, on_delete=models.CASCADE)
    title = models.CharField(max_length=200)
    body = models.TextField()
    category = models.ForeignKey(Category, on_delete=models.CASCADE)

And this is my view

class ArticleListCreateGet(ListAPIView, CreateAPIView):
    queryset = Article.objects.all()
    serializer_class = ArticleSerializer

and this my url

path('api/v1/article', views.ArticleListCreateGet, name='article'),

I am not getting whats wrong with my code, can anyone tell me why i am seeing above error?

Upvotes: 0

Views: 134

Answers (1)

dfundako
dfundako

Reputation: 8324

Your path is referring to views.ArticleListCreateGet which is a class based view, not a function.

Try views.ArticleListCreateGet.as_view() in your path and see what happens.

Upvotes: 1

Related Questions