rest framework 尝鲜

安装

pip install djangorestframework

新建项目

python manage.py startapp idcs

添加模型(models.py)

class Idcs(model.Model):
    name =CharField('机房名称',max_length=20)
    address  = models.CharField('机房地址',max_length=200)
    phone    = models.CharField('机房联系电话',max_length=15)
    email    = models.EmailField('机房联系 email')
    letter   = models.CharField('idc字母简称',max_length=5)

新建序列化文件(serializer.py)

from from rest_framework import ModelSerializer
from .models import Idcs
class IdcSerializers(serializers.ModelSerializer):
    class Meta:
    model = Idcs
    field = ('__all__')
#ModelSerializer是serializers的封装,简单实现了create,update方法

修改视图文件(views.py)

from  rest_framework import viewsets
from .models import Idcs
from .serializer import IdcSerializers

class IdcListViewset(viewsets.ModelViewSet):
    queryset = Idcs.objects.all()
    serializer_class = IdcSerializers

修改顶层urls

from django.conf.urls import url,include
urlpatterns = [
    url(r'^',include('idcs.urls')),
]

修改idcs app路由

from django.conf.urls import url,include
from  rest_framework.routers import DefaultRouter
route = DefaultRouter()
route.register("idcs",views.IdcListViewset)

urlpatterns = [
    url(r'^',include(route.urls))
]

启动服务并访问

123.png

234.png

你可能感兴趣的:(rest framework 尝鲜)