🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
文档链接[https://docs.djangoproject.com/zh-hans/4.2/](https://docs.djangoproject.com/zh-hans/4.2/) 安装django环境 ``` python -m pip install Django python -m pip install Django==4.0 ``` 创建项目 ``` D:\tools\Python\Anaconda\envs\python3.10.6\Scripts\django-admin.exe startproject app ``` pycharm 创建项目 ![](https://img.kancloud.cn/d3/3e/d33eec4de4523f1bc9c582c4815e66ef_886x614.png) 创建模块 ``` D:\tools\Python\Anaconda\envs\python3.10.6\python.exe manage.py startapp admin ``` mysql扩展安装 ``` python -m pip install mysqlclient ``` 数据库迁移 ``` D:\tools\Python\Anaconda\envs\python3.10.6\python.exe manage.py makemigrations ``` ``` D:\tools\Python\Anaconda\envs\python3.10.6\python.exe manage.py migrate ``` 添加表注释 ``` D:\tools\Python\Anaconda\envs\python3.10.6\python.exe -m pip install addcomments INSTALLED_APPS = [ "addcomments", ] D:\tools\Python\Anaconda\envs\python3.10.6\python.exe manage.py addcolumncomments ``` ![](https://img.kancloud.cn/51/48/51484c789fe6cd516cdbc51c30597827_666x223.png) Django-restframework安装 ``` D:\tools\Python\Anaconda\envs\python3.10.6\python.exe -m pip install djangorestframework INSTALLED_APPS = [ "rest_framework", ] ``` ``` ~~~ from rest_framework.generics import GenericAPIView from rest_framework.mixins import ListModelMixin from rest_framework.viewsets import ViewSetMixin from admin.models import Admin as AdminModel from rest_framework import serializers from rest_framework.pagination import PageNumberPagination, LimitOffsetPagination, CursorPagination class AdminSerializers(serializers.ModelSerializer): class Meta: model = AdminModel fields = '__all__' class CommonPageNumberPagination(PageNumberPagination): # 有4个类属性 # 每页显示条数 page_size = 20 # 分页查询的那个参数 ?page=10 page_query_param = 'page' # ?page=3&size=3 查询3页,每页查询3条 page_size_query_param = 'size' # 可以通过size控制每页显示的条数,但是通过这个参数控制最多显示多少条 max_page_size = 1000 class Admin(ViewSetMixin, GenericAPIView, ListModelMixin): queryset = AdminModel.objects.all() serializer_class = AdminSerializers # pagination_class后面是我们自己写的类,只不过在导入的时候我们重新命名了 pagination_class = CommonPageNumberPagination ~~~ ``` django-filter模糊查询 ~~~ D:\tools\Python\Anaconda\envs\python3.10.6\python.exe -m pip install django-filter ~~~