How can I write function in using rest_framework

I want to processing posted api data in How can I do it. I am getting error. My

from django.shortcuts import render, redirect

from rest_framework import viewsets
from rest_framework.decorators import api_view
from rest_framework.response import Response

from .models import Student, Teacher
from .serializer import StudentSerializer, TeacherSerializer

class StudentView(viewsets.ModelViewSet):

  @api_view(['GET', 'POST'])
  def index(self,request):
    if request.method == 'GET':
      queryset = Student.objects.all()
      serializer_class = StudentSerializer
    elif request.method == 'POST':

class TeacherView(viewsets.ModelViewSet):
  queryset = Teacher.objects.all()
  serializer_class = TeacherSerializer


from django.contrib import admin
from django.urls import path, include
from students import views
from rest_framework import routers

router = routers.DefaultRouter(trailing_slash = False)
router.register(r'studentdetails', views.StudentView, basename='studentdetails')
router.register(r'teacherdetails', views.TeacherView, basename='teacherdetails')

urlpatterns = [
    path('', include(router.urls))

when I'm opening http://localhost:7777/studentdetails I'm getting error: 'StudentView' should either include a queryset attribute, or override the get_queryset() method.

How can I write it correct?

Back to Top