位置参数的Django Rest API类型错误

时间:2019-06-21 18:00:36

标签: django django-rest-framework

Django解雇了我这个错误,我不明白为什么。

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

这是我的序列化器类:

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

这些是我的模特

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)

这是我的观点

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

这是我的网址

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

我的代码没有出什么毛病,有人可以告诉我为什么我看到上述错误吗?

1 个答案:

答案 0 :(得分:1)

您的路径引用 leakfunction <- function() { x <- runif(100000, 0, 1) a ~ b } ,它是基于类的视图,而不是函数。

在路径中尝试views.ArticleListCreateGet,看看会发生什么。