作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经将 django rest 框架版本 3.10 集成到现有的 django 2.2 项目中,将 api root 放在 /api
。
现在我正在尝试使用 coreapi cli 客户端将一些文档上传到服务器。
$ coreapi get http://localhost:8000/openapi
<DownloadedFile '/root/.coreapi/downloads/openapi (4)', open 'rb'>
$ coreapi get http://localhost:8000/api
{
"invoices": "http://localhost:8000/api/invoices/"
}
$ coreapi action invoices list
Index ['invoices']['list'] did not reference a link. Key 'invoices' was not found.
/openapi
是根据请求生成模式并返回的端点
openapi: 3.0.2
info:
title: Orka
version: TODO
description: API for orka project
paths:
/invoices/:
get:
operationId: ListInvoices
parameters: []
responses:
'200':
content:
application/json:
schema:
required:
- file_name
- original_file_name
properties:
file_name:
type: string
original_file_name:
type: string
maxLength: 80
upload_date:
type: string
format: date-time
readOnly: true
data:
type: object
nullable: true
confidence:
type: number
user_verified:
type: boolean
/invoices/{id}/:
get:
operationId: retrieveInvoice
parameters:
- name: id
in: path
required: true
description: A unique integer value identifying this Invoice.
schema:
type: string
responses:
'200':
content:
application/json:
schema:
required:
- file_name
- original_file_name
properties:
file_name:
type: string
original_file_name:
type: string
maxLength: 80
upload_date:
type: string
format: date-time
readOnly: true
data:
type: object
nullable: true
confidence:
type: number
user_verified:
type: boolean
/api/invoices
)。
# urls.py
from rest_framework import routers
from . import views
router = routers.DefaultRouter()
router.register(r'invoices', views.InvoiceViewSet)
urlpatterns = [
path('api/', include(router.urls)),
]
# views.py
# ... imports ...
class InvoiceSerializer(serializers.HyperlinkedModelSerializer):
"""Defines API representation of invoices"""
class Meta: # pylint:disable=too-few-public-methods, missing-docstring
model = Invoice
fields = (
'file_name',
'original_file_name',
'upload_date',
'data',
'confidence',
'user_verified',
)
class InvoiceViewSet(viewsets.ModelViewSet):
"""Defines api views for invoices"""
# default permissions are set in settings.py
parser_classes = (JSONParser, XMLParser, FormParser, MultiPartParser)
queryset = Invoice.objects.all()
serializer_class = InvoiceSerializer
@action(methods=['post'], detail=True)
def upload_with_ground_truth_file(self, request, pk):
pass
最佳答案
我遇到了同样的错误——python 3.8.5、Django 3.1、DRF 3.12.1。对我来说,相似之处在于 coreapi
返回一个 DownloadedFile
而不是 Document
,这似乎发生在 coreapi
收到它不期望的内容类型时。
对我来说,解决方案是:
pip install openapi-codec
(如果你还没有安装) coreapi get http://localhost:8000/openapi?format=openapi-json --format=openapi
DownloadedFile
的内容,而是看到可用标签/操作的摘要。
关于python-3.x - 如何在 django rest 框架中使用 coreapi 客户端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57061803/
我是一名优秀的程序员,十分优秀!