首页 > 其他 > 详细

rest framework 尝鲜

时间:2018-06-16 16:49:22      阅读:187      评论:0      收藏:0      [点我收藏+]

安装

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 尝鲜

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!