from django.conf.urls import url from django.contrib import admin from django.urls import path,include from rest_framework.routers import DefaultRouter from rest_framework.documentation import include_docs_urls from idcs.views import IdcsVi
from django.contrib import admin
from django.urls import path,include
from rest_framework.routers import DefaultRouter
from rest_framework.documentation import include_docs_urls
from idcs.views import IdcsViewSet
route = DefaultRouter()
route.register("idcs",IdcsViewSet,basename="idcs")
urlpatterns = [
url(r'^', include(route.urls)), url(r'^docs/', include_docs_urls("运维平台接口文档")),
]from django.shortcuts import render
# Create your views here.from rest_framework.viewsets import ModelViewSet
from idcs.serializers import IdcSerializers
from .models import idcs
class IdcsViewSet(ModelViewSet):
""" list:获取数据
destroy:删除数据
""" queryset = idcs.objects.all()
serializer_class = IdcSerializersfrom rest_framework import serializers
from .models import *
class IdcSerializers(serializers.Serializer):
""" IDC序列化类
""" id = serializers.IntegerField(read_only=True)
name = serializers.CharField(required=True,max_length=30,label="IDC名称",help_text="IDC名称")
address = serializers.CharField(required=True,max_length=30,label="IDC地址",help_text="IDC地址")
phone = serializers.CharField(required=True,max_length=30,label="IDC电话",help_text="IDC电话")
email = serializers.EmailField(required=True,label="IDC联系人邮箱",help_text="IDC联系人邮箱")
letter = serializers.CharField(required=True,max_length=30,label="idc简称",help_text="idc简称")
def create(self, validated_data):
return idcs.objects.create(**validated_data)
def update(self,instance,validated_data):
instance.name = validated_data.get("name",instance.name)
instance.address = validated_data.get("address", instance.address)
instance.phone = validated_data.get("phone", instance.phone)
instance.email = validated_data.get("email", instance.email)
instance.letter = validated_data.get("letter", instance.letter)
instance.save()
return instance
# class IdcsSerializer(serializers.ModelSerializer):# """图书数据序列化器"""# class Meta:# model = idcs# fields = '__all__'from django.db import models
# Create your models here.
class idcs(models.Model):
name = models.CharField(max_length=30,verbose_name="姓名")
address = models.CharField(max_length=30,verbose_name="姓名")
phone = models.CharField(max_length=30,verbose_name="姓名")
email = models.EmailField(verbose_name="邮件")
letter = models.CharField(max_length=30,verbose_name="IDC简称")
def __str__(self):
return self.name
class Meta:
db_table="idcs"