Home > Mobile >  Class-based Django API only GET method. It's possible?
Class-based Django API only GET method. It's possible?

Time:11-09

I'm building a class-based only GET method API with Django and Rest Framework. However, looking on internet, it seems to be impossible to do that.
Here is my urls.py:

from django.urls import path, include
from rest_framework import routers
from . import views

api_router = routers.DefaultRouter()
api_router.register(r"get_foo_id", views.GetFooIdViewSet)

urlpatterns = [
    path( "", include(api_router.urls)),
]

views.py

from app.models import *
from app.serializers import *
from rest_framework import viewsets
from django_filters.rest_framework import DjangoFilterBackend

class GetFooIdViewSet(viewsets.ModelViewSet):
    queryset = Foos.objects.all()
    serializer_class = FoosSerializer
    filter_backends = [DjangoFilterBackend]
    filterset_fields = ['foo_id']

It's really impossible?

CodePudding user response:

You can allow only GET(list) requests in urls.py. Try to do it:

path('your_path/', views.GetFooIdViewSet.as_view({
    'get': 'list'
})),
  • Related