def api_get_neighbour_post(request, img_id): """ 获取相邻的两个post(img_id+1,img_id-1) """ previous_post = Img.objects.filter(id__lt=img_id, status=status_normal).order_by('-id')[:1] next_post = Img.objects.filter(id__gt=img_id, status=status_normal).order_by('id')[:1] d = '{"previous":' + ToJson.queryset_to_json(previous_post) + ', next:' + ToJson.queryset_to_json(next_post) + '}' return JsonResponse.response(d)
def response(data): """ 把Model、QuerySet、String转换为json,并返回HttpResponse """ if data: if isinstance(data, Model): return HttpResponse(ToJson.model_to_json(data), content_type="application/json") elif isinstance(data, QuerySet): return HttpResponse(serializers.serialize('json', data), content_type="application/json") # elif isinstance(data, dict): # return HttpResponse(serializers.serialize('json', data), content_type="application/json") # elif isinstance(data, list): # return HttpResponse(serializers.serialize('json', data), content_type="application/json") else: # raise Exception('data is not Model or QuerySet') return HttpResponse(data, content_type="application/json") else: raise Exception('data is empty')