Django 自动生成api接口文档

安装第三方包

pip install coreapi

配置接口路径

from rest_framework.documentation import include_docs_urls

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'docs/', include_docs_urls(title='接口文档')),  # 接口路径 
]

在serializers.py文件设置备注

在序列化器的class Meta 里面的extra_kwargs里面添加额外备注:

class Meta:
        model = models.User
        fields = ('id','username','email','password','phone','pwdagain','messagecode','ischecked','token',)

        # 补充说明
        extra_kwargs= {
            'username':{
                'min_length': 6,
                'max_length': 12,
                'error_messages':{
                    'min_length': '6个字符',
                    'max_length': '12个字符',
                },
            },
            'password': {
                'help_text': '密码6-8位',
            }
        }

用浏览器打开api接口文档页面:

在这里插入图片描述
通过上面代码help_text补充说明后的接口文档部分图:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/dakengbi/article/details/92798049