TypeError:'str'对象不可调用-根据API请求

时间:2019-09-20 14:40:17

标签: python django python-3.x django-rest-framework

我正在尝试运行最小的Django服务器,但在向/ api / v1 /端点发送获取请求时收到此错误。

我知道之前也曾问过类似的问题,并且我相信这是由于导入参考无效而引起的,但是我看不到我在哪里犯了错误。

文件系统(减去缓存+迁移文件夹):

.
├── api
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── models.py
│   ├── serializers.py
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── blog_project
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── db.sqlite3
├── manage.py
├── Pipfile
├── Pipfile.lock
└── posts
    ├── admin.py
    ├── apps.py
    ├── __init__.py
    ├── models.py
    ├── tests.py
    └── views.py

api / views.py

from django.shortcuts import render

# Create your views here.
from rest_framework import generics

from posts import models
from .serializers import PostSerializer

class PostList(generics.ListCreateAPIView):
        queryset = models.Post.objects.all()
        serializer_class = PostSerializer

class PostDetail(generics.RetrieveUpdateDestroyAPIView):
        queryset = models.Post.objects.all()
        serializer_class = PostSerializer

api / serializers.py

from rest_framework import serializers
from posts import models


class PostSerializer(serializers.ModelSerializer):

        class Meta:
                fields = ('id', 'author', 'title', 'body', 'created_at',)
                model = models.Post

api / urls.py

from django.urls import path

from .views import PostList, PostDetail

urlpatterns = [
        path('<int:pk>/', PostDetail.as_view()),
        path('', PostList.as_view()),
]

posts / models.py

from django.db import models

# Create your models here.
from django.db import models
from django.contrib.auth.models import User

class Post(models.Model):
        author = models.ForeignKey(User, on_delete = models.CASCADE)
        title = models.CharField(max_length = 50)
        body = models.TextField()
        created_at = models.DateTimeField(auto_now_add = True)
        updated_at = models.DateTimeField(auto_now = True)

        def __str__(self):
                return self.title

完整追溯:

Traceback (most recent call last):
  File "/home/dario/anaconda3/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/dario/anaconda3/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/dario/anaconda3/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/dario/anaconda3/lib/python3.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/home/dario/anaconda3/lib/python3.7/site-packages/django/views/generic/base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/dario/anaconda3/lib/python3.7/site-packages/rest_framework/views.py", line 505, in dispatch
    response = self.handle_exception(exc)
  File "/home/dario/anaconda3/lib/python3.7/site-packages/rest_framework/views.py", line 465, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/home/dario/anaconda3/lib/python3.7/site-packages/rest_framework/views.py", line 476, in raise_uncaught_exception
    raise exc
  File "/home/dario/anaconda3/lib/python3.7/site-packages/rest_framework/views.py", line 493, in dispatch
    self.initial(request, *args, **kwargs)
  File "/home/dario/anaconda3/lib/python3.7/site-packages/rest_framework/views.py", line 411, in initial
    self.check_permissions(request)
  File "/home/dario/anaconda3/lib/python3.7/site-packages/rest_framework/views.py", line 331, in check_permissions
    for permission in self.get_permissions():
  File "/home/dario/anaconda3/lib/python3.7/site-packages/rest_framework/views.py", line 278, in get_permissions
    return [permission() for permission in self.permission_classes]
  File "/home/dario/anaconda3/lib/python3.7/site-packages/rest_framework/views.py", line 278, in <listcomp>
    return [permission() for permission in self.permission_classes]
TypeError: 'str' object is not callable

0 个答案:

没有答案