def get(self, request, *args, **kwargs): ''' 获取所有专题课信息 :param request: :param args: :param kwargs: :return: ''' # ret = {'code':1000,'data':None,'error':None} # 将字典封装成一个类,用ret.属性名去调用,简化代码 ret = BaseResponse() try: queryset = models.Course.objects.all() # 分页 page = PageNumberPagination() course_list = page.paginate_queryset(queryset, request, self) # 分页之后的结果序列化 # ser = CourseSerializer(course_list,many=True) ser = CourseModelSerializer(course_list, many=True) # ret['data'] = ser.data ret.data = ser.data except Exception as e: # ret['code'] = 500 ret.code = 500 # ret['error'] = '获取数据失败' ret.error = '获取数据失败' return Response(ret.dict)
def get(self, request, pk, *args, **kwargs): response = {'code': 1000, 'data': None, 'error': None} try: course = models.Course.objects.get(id=pk) ser = CourseModelSerializer(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 = models.Course.objects.get(id=pk) ser = CourseModelSerializer(instance=course) ret.data = ser.data except Exception as e: print(e) ret.get_error() return Response(ret.dict)
def get(self, request, pk, *args, **kwargs): ret = BaseResponse() try: course = models.Course.objects.filter(id=pk).first() ser = CourseModelSerializer(course) ret.data = ser.data except Exception as e: ret.code = 500 ret.error = '获取数据失败' return Response(ret.dict)
def get(self, request, pk, *args, **kwargs): ret = BaseResponse() try: course_data = models.DegreeCourse.objects.filter(pk=pk).first() ser = CourseModelSerializer(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 = BaseResponse() try: queryset = models.Course.objects.all() page = PageNumberPagination() course_list = page.paginate_queryset(queryset, request, self) ser = CourseModelSerializer(instance=course_list, many=True) ret.data = ser.data except Exception as e: ret.code = 500 ret.error = '获取数据失败' return Response(ret.dict)
def retrieve(self, request, pk, *args, **kwargs): ret = BaseReponse() try: course = models.Course.objects.get(id=pk) ser = CourseModelSerializer(instance=course) ret['data'] = ser.data except Exception as e: ret['code'] = 500 ret['error'] = '获取数据失败' return Response(ret.dict)
def get(self, request, *args, **kwargs): ret = BaseResponse() try: #get data from db queryset = models.Course.objects.all() #分页 page = PageNumberPagination() course_list = page.paginate_queryset(queryset, request, self) #分页后的结果执行序列化 ser = CourseModelSerializer(instance=course_list, many=True) ret.data = ser.data except Exception as e: ret.code = 500 ret.error = 'failed to get data' return Response(ret.dict)
def get(self, request, *args, **kwargs): # response = {'code':1000,'data':None,'error':None} ret = BaseResponse() try: # 从数据库获取数据 queryset = models.Course.objects.all() # 分页 page = PageNumberPagination() course_list = page.paginate_queryset(queryset, request, self) # 分页之后的结果执行序列化 ser = CourseModelSerializer(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): ret = BaseResponse() try: # 从数据库获取数据 queryset = models.Course.objects.all() # 分页 # page = PageNumberPagination() # course_list = page.paginate_queryset(queryset, request, self) # 分页之后的结果执行序列化 # print("345354",course_list) ser = CourseModelSerializer(instance=queryset, many=True) print("45646") ret.data = ser.data print("%%%%%%") except Exception as e: ret.code = 500 ret.error = "获取数据失败" print("qqqqqqqqq", e) return Response(ret.dict)
def get(self, request, *args, **kwargs): ret = BaseResponse() try: # 从数据库获取数据 query_set = models.Course.objects.all() # 分页 page = PageNumberPagination() course_list = page.paginate_queryset(query_set, request, self) # 分页后执行序列化 ser = CourseModelSerializer(instance=course_list, many=True) # 展示所有专业课 pro_cou = models.Course.objects.filter(degree_course__isnull=True) print(pro_cou) ret.data = ser.data except Exception as e: print(e) ret.get_error() return Response(ret.dict)
def get(self, request, *args, **kwargs): response = {'code': 100, 'data': None, 'error': None} try: # 从数据库获取数据 queryset = models.Course.objects.all() print(queryset) # 分页 # page = PageNumberPagination() # course_list = page.paginate_queryset(queryset,request,self) # 分页之后的结果执行序列化 ser = CourseModelSerializer(queryset, many=True) response['data'] = ser.data except Exception as e: response['code'] = 500 response['error'] = '获取数据失败' return Response(response)
def list(self, request, *args, **kwargs): course_list = models.Course.objects.all() ser = CourseModelSerializer(instance=course_list, many=True) return Response(ser.data)