码迷,mamicode.com
首页 > 其他好文 > 详细

django-rest-framework 自动生成接口说明文档

时间:2018-11-14 15:36:25      阅读:539      评论:0      收藏:0      [点我收藏+]

标签:ocs   queryset   alt   log   ges   frame   hostname   ati   cut   

自动生成接口说明文档

安装

pip install djangorestframework 

urls.py

from rest_framework.documentation import include_docs_urls

    path(‘docs/‘, include_docs_urls(title=‘文档‘)),

models.py

from django.db import models

# Create your models here.

class Asset(models.Model):

    hostname = models.CharField(max_length=64, verbose_name=‘主机名‘, unique=True)
    ip = models.CharField(max_length=30, verbose_name=‘ip‘, blank=True, null=True, )

    class Meta:
        db_table = "asset"
        verbose_name = "asset"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.hostname

serializers.py

from rest_framework import serializers
from .models import Asset

class AssetSerializer(serializers.ModelSerializer):
    hostname = serializers.CharField(help_text=‘主机‘)

    class Meta:
        model = Asset
        fields = ‘__all__‘

views.py

import json
from django.shortcuts import HttpResponse
from rest_framework import permissions
from rest_framework import generics
from rest_framework.views import APIView
from .serializers import AssetSerializer
from .models import Asset

class AssetInfo(generics.ListCreateAPIView):
    """
    资产
    """
    queryset = Asset.objects.get_queryset().order_by(‘id‘)
    serializer_class = AssetSerializer
    permission_classes = (permissions.IsAdminUser,)

docs

http://127.0.0.1:8000/docs/

技术分享图片

django-rest-framework 自动生成接口说明文档

标签:ocs   queryset   alt   log   ges   frame   hostname   ati   cut   

原文地址:http://blog.51cto.com/hequan/2316781

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!