def get(self,request,*args,**kwargs): #response = {'code':1000,'data':None,'error':None} ret = BaseResponse() try: # 从数据库获取数据 queryset = models.Course.objects.all() print(queryset) # 分页 page = PageNumberPagination() course_list = page.paginate_queryset(queryset,request,self) print(course_list) # 分页之后的结果执行序列化 ser = CourseSerializer(instance=course_list,many=True) print(ser.data) # response["data"] = ser.data ret.data = ser.data print("aaaaa",ret.dict) except Exception as e: ret.code = 500 ret.error = '获取数据失败' return Response(ret.dict)
def get(self, request, version): response = BaseResponse() try: if request.query_params.get('a'): queryset = Course.objects.filter(degree_course__isnull=True) else: queryset = Course.objects.all() p = CoursePagination() course_list = p.paginate_queryset(queryset=queryset, request=request, view=self) print(course_list) serializer = CourseSerializer(course_list, many=True) # serializer.data OrderedDict有序字典列表 print(serializer.data) response.data = serializer.data print() except Exception as e: response.code = 0 response.error = '获取数据失败' return Response(response.dict) # vars(object)
def get(self, request, pk, *args, **kwargs): response = {'code': 1000, 'data': None, 'error': None} try: course = models.Course.objects.filter(id=pk) ser = CourseSerializer(instance=course) response['data'] = ser.data except Exception as e: response['code'] = 500 response['error'] = '获取数据失败' return Response(response)
def get(self, request, pk, *args, **kwargs): ret = BaseResponse() try: course_data = models.Course.objects.get(pk=pk) ser = CourseSerializer(instance=course_data) ret.data = ser.data except Exception as e: ret.code = 500 ret.error = '獲取數據失敗' return Response(ret.dict)
def list(self, request, *args, **kwargs): ret = {'code': 1000, 'data': None} try: queryset = models.Course.objects.all() ser = CourseSerializer(instance=queryset, many=True) ret['data'] = ser.data except Exception as e: ret['code'] = 1001 ret['error'] = '获取课程失败' return Response(ret)
def get(self, request, *args, **kwargs): ret = BaseResponse() try: #从数据库取数据 queryset = models.Course.objects.all() ser = CourseSerializer(queryset, many=True) ret.data = ser.data except Exception as e: ret.code = 500 ret.error = "获取数据失败" return Response(ret.dict)
def post(self, request): try: data = extract_json_data(request) company = get_current_company(request) course_programs = data.get("course_programs", None) if not course_programs: raise_error("No Programs") if data.get("uuid", None): instance = Course.objects.get(pk=data.get("uuid")) serializer = CourseSerializer(data=data, instance=instance) else: data["company"] = company serializer = CourseSerializer(data=data) if serializer.is_valid(): instance = serializer.save() self.save_course_program(instance.pk, course_programs) else: raise_error(serializer.errors) return success_response() except Exception as e: print(e) return error_response(str(e), show_line=True)
def list(self, request, *args, **kwargs): """获取课程列表""" courses = { 'code': 1000, 'data': [], } try: # 在公司需要加分页 course_list = models.Course.objects.all() all_course = CourseSerializer(instance=course_list, many=True) courses['data'] = all_course.data except Exception as e: courses['code'] = 1001 courses['error'] = '获取课程列表失败' return Response(courses)
def get(self, request, *args, **kwargs): ret = BaseResponse() try: # 从数据库获取数据 queryset = models.Course.objects.all() # 分页 page = PageNumberPagination() course_list = page.paginate_queryset(queryset, request, self) # 分页之后的结果执行序列化 ser = CourseSerializer(instance=course_list, many=True) ret.data = ser.data except Exception as e: ret.code = 500 ret.error = '获取数据失败' return Response(ret.dict)
def get(self, request, *args, **kwargs): # 可利用request.version取版本號 ret = BaseResponse() try: course_data = models.Course.objects.all() # 分頁 page = PageNumberPagination() course_list = page.paginate_queryset(course_data, request, self) ser = CourseSerializer(instance=course_list, many=True) ret.data = ser.data except Exception as e: ret.code = 500 ret.error = '獲取數據失敗' return Response(ret.dict)
def list(self, request, *args, **kwargs): """ 获取课程列表的接口 :param request: :param args: :param kwargs: :return: """ ret = {"code": 1000, "data": None} try: queryset = models.Course.objects.all() ser = CourseSerializer(instance=queryset, many=True) ret["data"] = ser.data except Exception as e: ret["code"] = 1001 ret["error"] = "获取课程失败" return Response(ret)
def get(self, request, *args, **kwargs): # a.查看所有学位课并打印学位课名称以及授课老师 course_list = models.DegreeCourse.objects.values('name', 'teachers') # b.查看所有学位课并打印学位课名称以及学位课的奖学金 # c.展示所有的专题课 # d. 查看id=1的学位课对应的所有模块名称 # e.获取id = 1的专题课,并打印:课程名、级别(中文)、why_study、what_to_study_brief、所有recommend_courses # f.获取id = 1的专题课,并打印该课程相关的所有常见问题 # g.获取id = 1的专题课,并打印该课程相关的课程大纲 # h.获取id = 1的专题课,并打印该课程相关的所有章节 # ser = CourseSerializer(instance=course_list, many=True) return Response(ser.data)
def list(self, request, *args, **kwargs): course_list = models.Course.objects.all() ser = CourseSerializer(instance=course_list, many=True) return Response(ser.data)