From d2b971806f92b30ea0a56110f2d7dab2adf6f4c7 Mon Sep 17 00:00:00 2001 From: lrs <2776117553@qq.com> Date: Fri, 8 Sep 2023 16:38:35 +0800 Subject: [PATCH] Further improvement of teacher functions -Query student information --- EduSystemServer/teacher/urls.py | 3 +- EduSystemServer/teacher/views.py | 108 +++++++++++++++++++++++-------- 2 files changed, 84 insertions(+), 27 deletions(-) diff --git a/EduSystemServer/teacher/urls.py b/EduSystemServer/teacher/urls.py index ef54434..93b00b7 100644 --- a/EduSystemServer/teacher/urls.py +++ b/EduSystemServer/teacher/urls.py @@ -12,5 +12,6 @@ from .views import * urlpatterns = [ path("", teacher), path('delete/',delete), - path('student/',student_performance_management) + path('student/',student_performance_management), + path('query/',query_student_information), ] \ No newline at end of file diff --git a/EduSystemServer/teacher/views.py b/EduSystemServer/teacher/views.py index 696fe8a..988ac62 100644 --- a/EduSystemServer/teacher/views.py +++ b/EduSystemServer/teacher/views.py @@ -40,29 +40,20 @@ def teacher(request): dept = request.GET.get('dept') data = [] if name: - filtered = Teacher.objects.filter(name=name) - for teacher_x in filtered: - data.append(teacher_x.to_dict()) + data.append(Teacher.objects.filter(name=name)) elif sex: - filtered = Teacher.objects.filter(sex=sex) - for teacher_x in filtered: - data.append(teacher_x.to_dict()) + data.append(Teacher.objects.filter(sex=sex)) elif title: - filtered = Teacher.objects.filter(title=title) - for teacher_x in filtered: - data.append(teacher_x.to_dict()) + data.append(Teacher.objects.filter(title=title)) elif education: - filtered = Teacher.objects.filter(education=education) - for teacher_x in filtered: - data.append(teacher_x.to_dict()) + data.append(Teacher.objects.filter(education=education)) elif dept: - filtered = Teacher.objects.filter(dept=dept) - for teacher_x in filtered: - data.append(teacher_x.to_dict()) + data.append(Teacher.objects.filter(dept=dept)) else: - all_objects = Teacher.objects.all() - for teacher_x in all_objects: - data.append(teacher_x.to_dict()) + data.append(Teacher.objects.all()) + data_x = [] + for teacher_x in data[0]: + data_x.append(teacher_x.to_dict()) response = ResponseUtil.ok(data, "老师信息查询成功") return JsonResponse(response) @@ -90,16 +81,11 @@ def teacher(request): title = put[0]['title'] education = put[0]['education'] dept = put[0]['dept'] - Teacher.objects.filter(tid=tid).update(name=name) - Teacher.objects.filter(tid=tid).update(sex=sex) - Teacher.objects.filter(tid=tid).update(title=title) - Teacher.objects.filter(tid=tid).update(education=education) - Teacher.objects.filter(tid=tid).update(dept=dept) + Teacher.objects.filter(tid=tid).update(name=name,sex=sex,title=title) + Teacher.objects.filter(tid=tid).update(education=education,dept=dept) data = Teacher.objects.filter(tid=tid)[0].to_dict() return JsonResponse({'code': 200, 'msg': 'success','data':data}, safe=False) - - def delete(request): tid = request.GET.get('tid') try: @@ -110,7 +96,77 @@ def delete(request): @csrf_exempt def teaching_resources(request): - pass + if request.method == 'POST': + name = request.POST.get('name') + sid = request.POST.get('sid') + sex = request.POST.get('sex') + grade = request.POST.get('grade') + class_name = request.POST.get('class_name') + major = request.POST.get('major') + # 定义空查询条件 + conditions = Q() + if name: + conditions &= Q(name=name) + if sid: + conditions &= Q(sid=sid) + if sex: + conditions &= Q(sex=sex) + if grade: + conditions &= Q(grade=grade) + if class_name: + conditions &= Q(class_name=class_name) + if major: + conditions &= Q(major=major) + # 添加非空查询条件至基本查询集 + query = Student.objects.filter(conditions) + data = query.values() # 将 QuerySet 对象转换为字典列表 + data = list(data) # 转换为列表以便序列化为 JSON + print(query) + response = ResponseUtil.ok(data, "课程信息查询成功") + return JsonResponse(response) + elif request.method == "GET": + query = Student.objects.all() + data = query.values() # 将 QuerySet 对象转换为字典列表 + data = list(data) # 转换为列表以便序列化为 JSON + response = ResponseUtil.ok(data, "课程信息查询成功") + return JsonResponse(response) + +@csrf_exempt +def query_student_information(request): + if request.method == 'POST': + name = request.POST.get('name') + sid = request.POST.get('sid') + sex = request.POST.get('sex') + grade = request.POST.get('grade') + class_name = request.POST.get('class_name') + major = request.POST.get('major') + # 定义空查询条件 + conditions = Q() + if name: + conditions &= Q(name=name) + if sid: + conditions &= Q(sid=sid) + if sex: + conditions &= Q(sex=sex) + if grade: + conditions &= Q(grade=grade) + if class_name: + conditions &= Q(class_name=class_name) + if major: + conditions &= Q(major=major) + # 添加非空查询条件至基本查询集 + query = Student.objects.filter(conditions) + data = query.values() # 将 QuerySet 对象转换为字典列表 + data = list(data) # 转换为列表以便序列化为 JSON + print(query) + response = ResponseUtil.ok(data, "学生信息查询成功") + return JsonResponse(response) + elif request.method == "GET": + query = Student.objects.all() + data = query.values() # 将 QuerySet 对象转换为字典列表 + data = list(data) # 转换为列表以便序列化为 JSON + response = ResponseUtil.ok(data, "学生信息查询成功") + return JsonResponse(response) @csrf_exempt def student_performance_management(request): # 学生成绩管理 if request.method == "POST": # 学生成绩录入