美文网首页bifangback
bifangback-django界的丝袜哥

bifangback-django界的丝袜哥

作者: 万州客 | 来源:发表于2020-12-30 08:56 被阅读0次

现在swagger(丝袜哥)已是API界的标配,在API接口展示方面,确有一统天下之势。我们就先来简单的在django restframework中实现一下吧。

一,安装三方库

drf-yasg - Yet another Swagger generator

pip install drf-yasg
输出:

Installing collected packages: MarkupSafe, urllib3, jinja2, idna, chardet, certifi, uritemplate, ruamel.yaml.clib, requests, pyparsing, itypes, coreschema, ruamel.yaml, packaging, inflection, coreapi, drf-yasg
    Running setup.py install for coreschema ... done
Successfully installed MarkupSafe-1.1.1 certifi-2020.12.5 chardet-4.0.0 coreapi-2.3.3 coreschema-0.0.4 drf-yasg-1.20.0 idna-2.10 inflection-0.5.1 itypes-1.2.0 jinja2-2.11.2 packaging-20.8 pyparsing-2.4.7 requests-2.25.1 ruamel.yaml-0.16.12 ruamel.yaml.clib-0.2.2 uritemplate-3.0.1 urllib3-1.26.2

二,配置settings

INSTALLED_APPS = [
   ...
   'django.contrib.staticfiles', 
   'drf_yasg',
   ...
]

三,配置Urls

from rest_framework import permissions
from drf_yasg.views import get_schema_view
from drf_yasg import openapi

schema_view = get_schema_view(
   openapi.Info(
      title="毕方(Bifang)自动化部署平台 API",
      default_version='v1',
      description="毕方(Bifang)自动化部署平台,引导你进入devops开发的领域。",
      terms_of_service="https://github.com/aguncn/bifang",
      contact=openapi.Contact(email="aguncn@163.com"),
      license=openapi.License(name="BSD License"),
   ),
   public=True,
   permission_classes=(permissions.AllowAny,),

urlpatterns += [
   re_path(r'^swagger(?P<format>\.json|\.yaml)$', schema_view.without_ui(cache_timeout=0), name='schema-json'),
   path('swagger/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
   path('redoc/', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
]

四,弄一个user和group的API

1,序列化

from django.contrib.auth.models import Group
from django.contrib.auth import get_user_model
from rest_framework import serializers

User = get_user_model()


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User  # 要序列的model
        fields = ('url', 'username', 'email', 'groups')  # 数据字段


class GroupSerializer(serializers.ModelSerializer):
    class Meta:
        model = Group  # 要序列的model
        fields = ('url', 'name')  # 数据字段

2 api_views

from django.contrib.auth.models import User, Group
from rest_framework import viewsets
from .serializers import UserSerializer, GroupSerializer


class UserViewSet(viewsets.ModelViewSet):
    """
    用户接口
    """
    queryset = User.objects.all().order_by('-date_joined')  # 指定queryset
    serializer_class = UserSerializer  # 指定queryset对应的serializers


class GroupViewSet(viewsets.ModelViewSet):
    """
    用户组接口
    """
    queryset = Group.objects.all()  # 指定queryset
    serializer_class = GroupSerializer  # 指定queryset对应的serializers

3, urls路由配置

from django.urls import include, path
from rest_framework import routers
from .user_group_views import UserViewSet, GroupViewSet

# 使用router注册view,绑定url映射关系,
# 关于什么时候使用router,什么时候不能使用,后面奖路由的时候在深入了解吧
router = routers.DefaultRouter()
router.register(r'users', UserViewSet) # 绑定view到users路由下
router.register(r'groups', GroupViewSet)

urlpatterns = [
    path('', include(router.urls)),
]

五,查看API接口文档

http://127.0.0.1:8000/account/

2020-12-30 08_56_03-Api Root – Django REST framework.png

http://127.0.0.1:8000/swagger/

2020-12-30 08_53_49-.png

http://127.0.0.1:8000/redoc/

2020-12-30 08_54_43-毕方(Bifang)自动化部署平台 API.png

相关文章

网友评论

    本文标题:bifangback-django界的丝袜哥

    本文链接:https://www.haomeiwen.com/subject/ttmcoktx.html