def put(self, request, *args, **kwargs): data = request.data username = data.get('user') data["user"] = user_create(username) partial = kwargs.pop('partial', False) instance = self.get_object() serializer = self.get_serializer(instance, data=data, partial=partial, context={"stu_academy": "", 'stu_user': "", "stu_major": "", "stu_tutor": ""}) serializer.is_valid(raise_exception=True) self.perform_update(serializer) return Response(serializer.data)
def put(self, request, *args, **kwargs): data = request.data bulk = isinstance(data, list) if not bulk: username = data.get('stu_user') data["stu_user"] = user_create(username) data["stu_major"] = Major.objects.filter( maj_name=data.get('stu_major')).first() data["stu_academy"] = Academy.objects.filter( aca_cname=data.get('stu_academy')).first() data["stu_tutor"] = Tutor.objects.filter( tut_user__first_name=data.get('stu_tutor')).first() serializer = self.get_serializer(data=data, context={ "stu_academy": "", 'stu_user': "", "stu_major": "", "stu_tutor": "" }) else: for item in data: username = item['stu_user'] item["stu_user"] = user_create(username) data["stu_major"] = Major.objects.filter( maj_name=item['stu_major']).first() data["stu_academy"] = Academy.objects.filter( aca_cname=item['stu_academy']).first() data["stu_tutor"] = Tutor.objects.filter( user__first_name=data.get('stu_tutor')).first() serializer = self.get_serializer(data=data, many=True, context={ "stu_academy": "", 'stu_user': "", "stu_major": "", "stu_tutor": "" }) serializer.is_valid(raise_exception=True) serializer.save() return Response(serializer.data)
def post(self, request, *args, **kwargs): trans = trans_choice() file = request.data['file'] data = xlrd.open_workbook(filename=None, file_contents=file.read()) table = data.sheets()[0] nrows = table.nrows # 获取该sheet中的有效行数 s_list = list() for i in range(1, nrows): rowx = table.row_values(i) student_dict = dict() student_dict["stu_user"] = user_create(rowx[0], rowx[1]) student_dict["stu_name"] = rowx[0] student_dict["stu_number"] = rowx[1] student_dict["stu_candidate_number"] = rowx[2] student_dict["stu_card_type"] = rowx[3] student_dict["stu_cardID"] = rowx[4] student_dict["stu_gender"] = trans[rowx[5]] student_dict["stu_birth_day"] = datetime.strptime(str(int(rowx[6])), '%Y%m%d').strftime('%Y-%m-%d') student_dict["stu_nation"] = rowx[7] student_dict["stu_source"] = rowx[8] student_dict["stu_is_village"] = True if rowx[9] == "是" else False student_dict["stu_political"] = trans[rowx[10]] student_dict["stu_academy"] = Academy.objects.filter(aca_cname=rowx[11]).first() student_dict["stu_major"] = Major.objects.filter(maj_name=rowx[12]).first() student_dict["stu_class"] = Class.objects.filter(cla_name=rowx[14]).first() student_dict["stu_status"] = trans[rowx[15]] student_dict["stu_tutor"] = Tutor.objects.filter(tut_number=rowx[16]).first() student_dict["stu_type"] = trans[rowx[18]] student_dict["stu_learn_type"] = trans[rowx[19]] student_dict["stu_learn_status"] = trans[rowx[20]] student_dict["stu_grade"] = rowx[21] student_dict["stu_system"] = rowx[22] student_dict["stu_entrance_time"] = datetime.strptime(str(int(rowx[23])), '%Y%m').strftime('%Y-%m-01') student_dict["stu_cultivating_mode"] = trans[rowx[24]] student_dict["stu_enrollment_category"] = trans[rowx[25]] student_dict["stu_nationality"] = rowx[26] student_dict["stu_special_program"] = trans[rowx[27]] student_dict["stu_is_regular_income"] = True if rowx[28] == "是" else False student_dict["stu_is_tuition_fees"] = True if rowx[29] == "是" else False student_dict["stu_is_archives"] = True if rowx[30] == "是" else False student_dict["stu_graduation_time"] = datetime.strptime(str(int(rowx[31])), '%Y%m').strftime('%Y-%m-01') s_list.append(student_dict) serializer = self.get_serializer(data=s_list, many=True, context={"stu_academy": "", 'stu_user': "", "stu_major": "", "stu_tutor": ""}) serializer.is_valid(raise_exception=True) serializer.save() return Response(serializer.data)