完成搜索功能

main
roger_home_pc 11 months ago
parent 71aa137798
commit 6404c02310
  1. 7
      dingxin_toolbox_drf/settings.py
  2. 2
      update/serializers.py
  3. 8
      update/urls.py
  4. 16
      update/views.py

@ -114,8 +114,10 @@ LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai' TIME_ZONE = 'Asia/Shanghai'
USE_I18N = True USE_I18N = True
# USE_I18N = False
USE_TZ = True USE_TZ = True
# USE_TZ = False
# Static files (CSS, JavaScript, Images) # Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.2/howto/static-files/ # https://docs.djangoproject.com/en/4.2/howto/static-files/
@ -176,3 +178,8 @@ REST_FRAMEWORK = {
# 跨域配置 # 跨域配置
CORS_ALLOW_CREDENTIALS = True CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_ALLOW_ALL = True CORS_ORIGIN_ALLOW_ALL = True
# 默认时间格式
# DATE_FORMAT = '%Y-%m-%d' # 设置日期格式为年/月/日
# TIME_FORMAT = '%H:%M:%S' # 设置时间格式为小时:分钟
# DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S" # 设置日期时间格式为年/月/日 小时:分钟

@ -3,6 +3,8 @@ from update.models import Cinema
class CinemaSerializer(serializers.ModelSerializer): class CinemaSerializer(serializers.ModelSerializer):
update_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S')
class Meta: class Meta:
model = Cinema model = Cinema
fields = ('id', 'name', 'ip', 'zz_num', 'inner_id', 'db_user', 'db_pwd', 'sys_ver', 'client_ver', 'update_time') fields = ('id', 'name', 'ip', 'zz_num', 'inner_id', 'db_user', 'db_pwd', 'sys_ver', 'client_ver', 'update_time')

@ -14,7 +14,7 @@ Including another URLconf
1. Import the include() function: from django.urls import include, path 1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
""" """
from django.urls import path, include from django.urls import path, re_path, include
from update import views from update import views
from rest_framework.routers import DefaultRouter from rest_framework.routers import DefaultRouter
@ -22,5 +22,11 @@ router = DefaultRouter()
router.register('cinema', views.CinemaViewSet, 'cinema') router.register('cinema', views.CinemaViewSet, 'cinema')
urlpatterns = [ urlpatterns = [
path('cinema/search/', views.CinemaSearchAPIView.as_view()),
path('', include(router.urls)), path('', include(router.urls)),
# re_path(r'^cinema/search/ip/(?P<ip>[0-9.]*?)/version/(?P<version>[\S]*?)/$', views.CinemaSearchAPIView.as_view())
] ]
# &version=(?P<version>[\S]*?)

@ -1,6 +1,10 @@
from rest_framework import viewsets, permissions from django.http import JsonResponse
from django.db.models import Q
from rest_framework import viewsets, permissions, status
from update.models import Cinema from update.models import Cinema
from update.serializers import CinemaSerializer from update.serializers import CinemaSerializer
from rest_framework.views import APIView
from rest_framework.response import Response
class CinemaViewSet(viewsets.ModelViewSet): class CinemaViewSet(viewsets.ModelViewSet):
@ -15,3 +19,13 @@ class CinemaViewSet(viewsets.ModelViewSet):
queryset = Cinema.objects.all() queryset = Cinema.objects.all()
serializer_class = CinemaSerializer serializer_class = CinemaSerializer
permission_classes = (permissions.IsAuthenticated,) permission_classes = (permissions.IsAuthenticated,)
class CinemaSearchAPIView(APIView):
def get(self, request, *args, **kwargs):
query_params = request.query_params.dict()
print(query_params)
query_data = Cinema.objects.filter(
Q(ip__contains=query_params.get('ip')) & Q(sys_ver__icontains=query_params.get('version')))
serializer = CinemaSerializer(instance=query_data, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)

Loading…
Cancel
Save