from django.shortcuts import render
from .models import Student
from .serializers import StudentSerializer
from rest_framework.renderers import JSONRenderer
from django.http import HttpResponse, JsonResponse
# Model Object - Single Student Data

def student_detail(request, pk):
 stu = Student.objects.raw('SELECT * FROM mysqlapp_student')[pk]
 serializer = StudentSerializer(stu)
 json_data = JSONRenderer().render(serializer.data)
 # print(json_data)
 return HttpResponse(json_data, content_type='application/json')
 # return JsonResponse(serializer.data)

# Query Set - All Student Data
def student_list(request):
 #query = 'SELECT * FROM Student'
 stu = Student.objects.raw("SELECT * FROM mysqlapp_student;")
 #stu = Student.objects.all(query)
 # print(stu)
 serializer = StudentSerializer(stu, many=True)
 # print(serializer)
 # print(serializer.data)
 json_data = JSONRenderer().render(serializer.data)
 # print(json_data)
 return HttpResponse(json_data, content_type='application/json')
 # return JsonResponse(serializer.data, safe=False)
