class CourseSerializer(serializers.ModelSerializer): teacher = TeacherSerializer(read_only=True) participants = AppuntaStudentSerializer(read_only=True, many=True) class Meta: model = Course fields = ("id", 'name', 'teacher', 'participants')
class LevelDetailSerializer(serializers.ModelSerializer): lessons = LessonSerializer(read_only=True, many=True) teacher = TeacherSerializer(read_only=True) class Meta: model = Level fields = ('id', 'title', 'image', 'teacher', 'lessons') # def create(self, validated_data): # lesson_data = validated_data.pop('lessons') # teacher_data = validated_data.pop('teacher') # teacher = Users.objects.get_or_create() # Lesson.objects.create(level=level, teacher=teacher, **lesson_data) # # return level # def update(self, instance, validated_data): lesson_data = validated_data.pop('lessons') lessons = instance.lessons instance.title = validated_data.get('title') instance.image = validated_data.get('image') instance.teacher = validated_data.get('teacher') instance.save() for lesson_data in lesson_data: lesson = lessons.pop(0) lesson.title = lesson_data.get('title') lesson.description = lesson_data.get('description') lesson.video_url = lesson_data.get('video_url') lesson.material_url = lesson_data.get('material_url') lesson.save() return instance
def to_representation(self, instance): response = super().to_representation(instance) response['subject'] = SubjectSerializerId(instance.subject).data response['teacher'] = TeacherSerializer( instance.teacher).data['user']['first_name'] # print(instance.section) response['section'] = SectionSerializer(instance.section).data['name'] return response
class CourseTeacherSerializer(serializers.ModelSerializer): teacher = TeacherSerializer() grade = GradeSerializer() class Meta: model = Course fields = [ "id", "name", "teacher", "grade", ]
def get(self, request, *args, **kwargs): if request.GET.get("department") and request.GET.get("language"): try: r = TeacherSerializer( Teacher.objects.filter( department=request.GET.get("department"), known_langs=request.GET.get("language"), ), many=True, ).data except Teacher.DoesNotExist: r = {[]} return Response(r)
def teachers_list(request): if request.method == 'GET': teachers = TeacherInfo.objects.filter(is_teaching=True) serializer = TeacherSerializer(teachers, many=True) return Response(serializer.data) elif request.method == 'POST': serializer = TeacherSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response({'error': serializer.errors}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def teacher_detail(request, teacher_id): try: teacher = TeacherInfo.objects.get(id=teacher_id) except TeacherInfo.DoesNotExist as e: return Response({'error': str(e)}) # Get one objects if request.method == 'GET': serializer = TeacherSerializer(teacher) return Response(serializer.data) # Update selected objects elif request.method == 'PUT': serializer = TeacherSerializer(instance=teacher, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response({'error': serializer.errors}) # Delete selected object elif request.method == 'DELETE': teacher.delete() return Response({'deleted': True})
def post(self, request): """ 根据路由中班级id获取相关学生考勤 路由: POST attendance/user_attendance/ 请求: user_id = ? usrkind = ? """ # 获取请求参数 attendance = request.data userkind = attendance.getlist("userkind")[0] user_id = attendance.getlist("user_id")[0] # 教师 if userkind == "1": try: user_attendance = teacher_attendance_table.objects.filter( atuser_id=user_id) except: return Response({"error": "查询失败"}) # 序列化 tea_attendance_serializer = TeacherAttendanceSerializer( user_attendance, many=True) tea_attendance_dict = tea_attendance_serializer.data attendance_list = [] for dict1 in tea_attendance_dict: id = dict1["id"] time = dict1["attime"] status = dict1["atstatus"] lesson_id = dict1["alesson"] user_id = dict1["atuser"] if status == 1: status = "全勤" elif status == 2: status = "迟到" elif status == 3: status = "早退" elif status == 4: status = "缺勤" elif status == 5: status = "请假" # 获取老师姓名 teacher = teacher_table.objects.filter(tid=user_id) teacher_data = TeacherSerializer(teacher, many=True) teacher_name = teacher_data.data[0]["tname"] # 获取课程名 lesson = lesson_table.objects.filter(lid=lesson_id) lesson_data = LessonSerializer(lesson, many=True) lesson_name = lesson_data.data[0]["lname"] data = { "id": id, "name": teacher_name, "time": time, "lesson_name": lesson_name, "status": status, } attendance_list.append(data) return Response(attendance_list) # 学生 if userkind == "2": try: user_attendance = student_attendance_table.objects.filter( asuser_id=user_id) except: return Response({"error": "查询失败"}) # 序列化 stu_attendance_serializer = StudentAttendanceSerializer( user_attendance, many=True) stu_attendance_dict = stu_attendance_serializer.data attendance_list = [] for dict1 in stu_attendance_dict: id = dict1["id"] time = dict1["astime"] status = dict1["asstatus"] lesson_id = dict1["alesson"] user_id = dict1["asuser"] if status == 1: status = "全勤" elif status == 2: status = "迟到" elif status == 3: status = "早退" elif status == 4: status = "缺勤" elif status == 5: status = "请假" # 获取学生姓名 student = student_table.objects.filter(sid=user_id) student_data = StudentSerializer(student, many=True) student_name = student_data.data[0]["sname"] # 获取课程名 lesson = lesson_table.objects.filter(lid=lesson_id) lesson_data = LessonSerializer(lesson, many=True) lesson_name = lesson_data.data[0]["lname"] data = { "id": id, "name": student_name, "time": time, "lesson_name": lesson_name, "status": status, } attendance_list.append(data) return Response(attendance_list)