Python django + vue 实现DRF序列化模板

  1. 数据库模板 在models.py下边

class ConnectionTable(models.Model):

Name = models.CharField(max_length=1024, blank=True, null=True)

local_addrs = models.CharField(max_length=1024, blank=True, null=True) #本机地址

     2.  序列化器  在serializers.py 文件下

class ConnectionTableSerializer(serializers.ModelSerializer):

class Meta:

model = ConnectionTable

fields = "__all__"

id = serializers.SerializerMethodField()

def get_id(self,obj):

return obj.pk

3.   视图层 ,一般放在 views.py 或者viewsrest.py下面

class ConnectionTableViewSet(viewsets.ModelViewSet):

"""

获取

"""

queryset = ConnectionTable.objects.all()

serializer_class = ConnectionTableSerializer

filter_backends = (filters.DjangoFilterBackend,)

#创建

def create(self, request, *args, **kwargs):                          axios.post

servermanage_sync()

#更新

def update(self, request, *args, **kwargs):                           axios.put + [id]

servermanage_sync(request)

。。。

#序列化

def destroy(self, request, *args, **kwargs):                        

servermanage_sync(request)                       

#查询

def list(self, request, *args, **kwargs):         list  是可以axios.get直接请求数据库的内容

类似于:

if(this.form['id'])//修改

{

api = "/netattri/rest/ipnetwork/" + this.form['id'] + "/"

axios.put(api, this.form).then(function (response) {

    if (response.status == 200){

      app.$message.success('修改成功!');

.............................

}else //创建

{

api = "/netattri/rest/ipnetwork/"

axios.post(api, this.form).then(function (response) {

   if (response.status == 201){

      app.$message.success('提交成功!');

.......................

}

// 读取

var api = "/netattri/rest/ipnetwork/";

this.loading = true;

axios.get(api).then(function (response) {

.............

}

   4. admin.py  书写数据同步的方法

apps/

servermanage_app/

template/

serverconf.template

static/

component/

connectionaddmode.vue

        5. 添加请求接口的路由   在urls.py 里面

 如果想要请求 路径 +rest+ 接口名称来访问数据库的表,还需要把router 写到rest/里面

类似于前两条:

urlpatterns = [

url(r'^$', views.Tview.as_view()),

url(r'^rest/', include(router.urls)),

url(r'^tab_tunnelroute/$', views.TunnelRouteView.as_view()),

]

下边是添加路由:

url(r'^tab_tunnelroute/$', views.TunnelRouteView.as_view()),

router = routers.DefaultRouter()

router.register(r'ipnetwork', views.IpnetworkTableViewSet)

你可能感兴趣的:(django,vue.js)