项目级-使用Django做api踩过的坑...

Posted 大圣贰奇

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了项目级-使用Django做api踩过的坑...相关的知识,希望对你有一定的参考价值。

第一步

# 安装 \'django_filters\',\'rest_framework\',
pip3 install django_filters
pip3 install rest_framework

rest_framework与django_filters使用不在赘述,请参照Django REST Framework教程:快速入门 Rest-framework快速实现增删改查及筛选

第二步

在setting中配置INSTALLED_APPS 切记

 

 

setting加入配置

REST_FRAMEWORK = {
    \'DEFAULT_PERMISSION_CLASSES\':[
        \'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly\'
    ],
    \'DEFAULT_PAGINATION_CLASS\':\'rest_framework.pagination.PageNumberPagination\',
    # \'PAGE_SIZE\':3  (可先注释,加页码会用到)
}

 

 

第三步

在应用目录下创建 query_filter.py 与 serializers.py文件

# Author:jum
# -*- coding: utf-8 -*-
# @Time : 2020-03-10 9:20
# @File : query_filter.py
# @Software: PyCharm
"""筛选条件"""
# fields中为可筛选字段
# 切记图片字段不可加入筛选字段
from django_filters import rest_framework as filters from apps.user_blog.models import * class BussFilter(filters.FilterSet): class Meta: model = Buss fields = [\'id\', \'name\'] class GoodsFilter(filters.FilterSet): class Meta: model = Goods fields = [\'id\', \'buss_cate\',]
# Author:jum
# -*- coding: utf-8 -*-
# @Time : 2020-03-09 14:22
# @File : serializers.py
# @Software: PyCharm
# 用于返回json数据
# fields 中设置你将展示的字段
from rest_framework import serializers
from apps.user_blog.models import *



class StudentSerializers(serializers.ModelSerializer):
    class Meta:
        model = User  # 指定的模型类
        fields = (\'name\', \'job_num\')  # 需要序列化的属性


class BussSerializers(serializers.ModelSerializer):
    class Meta:
        model = Buss  # 指定的模型类
        fields = (\'id\', \'name\', \'pwd\', \'weight\', \'create_time\', \'update_time\')  # 需要序列化的属性


class GoodsSerializers(serializers.ModelSerializer):
    class Meta:
        model = Goods  # 指定的模型类
        fields = (\'id\', \'buss_cate\', \'name\', \'tp\', \'price\', \'buys_count\', \'weight\', \'create_time\')

第四步:

配置url,填充view

#view.py

from django.shortcuts import render
from rest_framework import viewsets
from apps.user_blog.models import *
from .serializers import StudentSerializers, BussSerializers,GoodsSerializers
from .query_filter import BussFilter,GoodsFilter
from django_filters import rest_framework as filters


# Create your views here.


class StudentViewSet(viewsets.ModelViewSet):
    # 指定结果集并设置排序
    # queryset 为查询集,可自定义
    queryset = User.objects.all()
    # 指定序列化的类
    serializer_class = StudentSerializers


class BussViewSet(viewsets.ModelViewSet):
    # 指定结果集并设置排序
    # queryset 为查询集,可自定义
    queryset = Buss.objects.all()  # 查询出所有商家
    # 指定序列化的类
    serializer_class = BussSerializers
    #加入筛选字段
    filter_backends = (filters.DjangoFilterBackend,)
    filter_class = BussFilter


class GoodsViewSet(viewsets.ModelViewSet):
    # 指定结果集并设置排序
    # queryset 为查询集,可自定义
    queryset = GoodsSerializers.objects.all()  # 查询出所有商家
    # 指定序列化的类
    serializer_class = GoodsSerializers
    filter_backends = (filters.DjangoFilterBackend,)
    filter_class = GoodsFilter
# Create your views here.
#url.py

from django.conf.urls import include, url
from rest_framework import routers
from . import views


# 定义路由地址
route = routers.DefaultRouter()

# 注册新的路由地址
route.register(r\'user\', views.StudentViewSet)
route.register(r\'buss\', views.BussViewSet)
route.register(r\'goods\', views.GoodsViewSet)

# 注册上一级的路由地址并添加
urlpatterns = [
    url(r\'^\', include(route.urls)),
]

最后,如过项目发布后没有静态文件 以nginx为例

 

 

 

 为rest_framework添加静态文件指向

location /static/rest_framework/ {        alias /usr/local/lib/python3.5/distpackages/rest_framework/static/rest_framework/;}

ps:分享不宜,此文对您有帮助或喜欢请点赞,不喜勿喷...

 

以上是关于项目级-使用Django做api踩过的坑...的主要内容,如果未能解决你的问题,请参考以下文章

做项目管理踩过的坑

那些年,接口测试踩过的坑~

最近项目中使用Spring data jpa 踩过的坑

Swift5 踩过的坑和奇怪的API笔记

Swift5 踩过的坑和奇怪的API笔记

Swift5 踩过的坑和奇怪的API笔记