【问题标题】:Django Rest Framework include_docs_urls adding _0 to actionDjango Rest Framework include_docs_urls 将 _0 添加到操作中
【发布时间】:2025-12-04 04:00:01
【问题描述】:

我们的 Django(Rest 框架)应用程序中有 url,例如:

r'^endpoint/(?P<item>[a-z_-]+)/$'
r'^endpoint/(?P<item>[a-z_-]+)/(?P<version>[0-9]+(\.[0-9])?)/$'

两者都有可用的 POST 方法。

一段时间以来,我们一直在使用 Swagger 来记录我们的 API,但想查看 Django Rest Framework 中包含的 coreapi 文档。

根据上述结构浏览我们的文档,coreapi 操作会导致:

# Initialize a client & load the schema document
client = coreapi.Client()
schema = client.get("http://localhost:8081/docs/")

# Interact with the first url
action = ["app", "endpoint &gt; create"]

# Interact with the second url
action = ["app", "endpoint &gt; create_0"]

我可以理解create_0 的来源,但理想情况下它会将关键字名称添加为后缀,例如create_version.

这可能吗?

【问题讨论】:

    标签: django django-rest-framework core-api


    【解决方案1】:

    两个关键字紧挨着似乎是个问题。

    r'^endpoint/(?P<item>[a-z_-]+)/$'
    r'^endpoint/(?P<item>[a-z_-]+)/(?P<version>[0-9]+(\.[0-9])?)/$'
    

    应替换为:

    r'^endpoint/(?P<item>[a-z_-]+)/$'
    r'^endpoint/(?P<item>[a-z_-]+)/version/(?P<version>[0-9]+(\.[0-9])?)/$'
    

    这会给你:

    action = ["endpoint", "item > version > create"]
    

    看起来更干净。

    【讨论】:

      最近更新 更多