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

rest framework 尝鲜

时间:2018-06-16 17:01:21      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:dcs   封装   启动服务   strip   ges   views   models   date   地址   

安装

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))
]

启动服务并访问

技术分享图片
技术分享图片

rest framework 尝鲜

标签:dcs   封装   启动服务   strip   ges   views   models   date   地址   

原文地址:https://www.cnblogs.com/guigujun/p/9190784.html

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