Django 是用Python開發的一個免費開源的Web框架,可以用於快速搭建高性能,優雅的網站後台
引入models
from django.db import models, connection
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
引入庫
from rest_framework import serializers
from .models import terminalModel #此為models創建的class
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__'
引入庫
import django_filters
from .models import terminalModel #此為models創建的class
class terminalFilter(django_filters.rest_framework.FilterSet):
result=django_filters.CharFilter(field_name='result',lookup_expr='icontains')
class Meta:
model = terminalModel
fields = ['result']
引入庫、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
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__'
可以創建多個視圖類,在這我只創建一個
引入庫、model、serializers、filter
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from .apis import getTerminal
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配置方面的東西我就不寫了,大家可以自行百度,個人覺得不難。
本人還在上學,第一次嘗試寫博客,如有寫得不好的地方請大家多多指出,謝謝大家!