Django访问数据库获取数据
前言
一、django是什么?
Django 是用Python开发的一个免费开源的Web框架,可以用于快速搭建高性能,优雅的网站后台
二、models使用
1.在所创建的app文件夹下创建models.py文件
引入models
from django.db import models, connection
2.初始化
class terminalModel(models.Model):
id = models.IntegerField(verbose_name='id',primary_key=True)
device_uuid = models.CharField(verbose_name='device_uuid', max_length=64)
terminal_name = models.CharField(verbose_name='terminal_name', max_length=64)
ip_addr = models.CharField(verbose_name='ip', max_length=64)
address = models.CharField(verbose_name='address', max_length=64)
video_stream_addr = models.CharField(verbose_name='video_stream_addr', max_length=64)
alg_instance = models.ForeignKey('alg_instancemodel', to_field='id', on_delete=models.CASCADE)
class Meta:
db_table = 'terminal_info' #对应数据表
verbose_name = 'name'
verbose_name_plural = verbose_name
def __str__(self):
return self.result
三、Serializers序列化
1.在所创建的app文件夹下创建serializers.py文件
引入库
from rest_framework import serializers
from .models import terminalModel #此为models创建的class
2.序列化
class TerminalSerializer(serializers.ModelSerializer):
class Meta:
model = terminalModel
fields = ['id','device_uuid', 'terminal_name' , 'ip_addr' , 'address' , 'video_stream_addr','alg_instance']
depth = 2
# 如果想全部输出可以 fields = '__all__'
四、filter使用
1.在所创建的app文件夹下创建filter.py文件
引入库
import django_filters
from .models import terminalModel #此为models创建的class
2.引入models
class terminalFilter(django_filters.rest_framework.FilterSet):
result=django_filters.CharFilter(field_name='result',lookup_expr='icontains')
class Meta:
model = terminalModel
fields = ['result']
五、构造api
1.在所创建的app文件夹下创建apis.py文件
引入库、model、serializers、filter
from rest_framework import mixins
from rest_framework import status
from rest_framework import viewsets
from rest_framework.response import Response
from rest_framework.pagination import PageNumberPagination
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import filters
from .filter import terminalFilter
from .serializers import TerminalSerializer
from .models import terminalModel #此为models创建的class
2.构造api视图
class GeneralPagination(PageNumberPagination):
# 分页设置类
page_size = 20
page_size_query_param = 'page_size'
page_query_param = 'page'
max_page_size = 500
class getTerminal(
mixins.RetrieveModelMixin,
mixins.ListModelMixin,
viewsets.GenericViewSet):
queryset = terminalModel.objects.all()
serializer_class = TerminalSerializer
pagination_class = GeneralPagination
filter_class = terminalFilter
filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
filter_fields = '__all__'
可以创建多个视图类,在这我只创建一个
六、设置api路由
1.在所创建的app文件夹下创建urls.py文件
引入库、model、serializers、filter
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from .apis import getTerminal
2.设置路由
app_name = "alg_manager" # 此为你的 app 项目
# app_name will help us do a reverse look-up latter.
router = DefaultRouter()
router.register('terminal_info',getTerminal,basename='terminal_info')
urlpatterns = [
path('', include(router.urls)),
]
最后在最外层的总路由url-urlpatterns里添加app路由就完成了
path('api/', include(urls))
总结
效果展示:
还有一些别的接口,我就不一一写了
心得
以上是我实习的时候做的一个小demo,主要是运用Django、Rest_framework后台框架编写接口访问数据库获取数据
有很多django配置方面的东西我就不写了,大家可以自行百度,个人觉得不难。
本人还在上学,第一次尝试写博客,如有写得不好的地方请大家多多指出,谢谢大家!