Mapping DRF GenericViewSet to http verbs via DefaultRouter

My goal is to implement adding a recipe to current user favorites list and removing it using Django Rest Framework 3.12.x.

POST api/recipes/{int:recipe_id}/favorite
DELETE api/recipes/{int:recipe_id}/favorite

Request body is empty. User_id is got from self.context[‘request’].user.id.

I.e. I need to serve POST and DELETE on the same URL.

I implemented that against DefaultRouter in conjunction with GenericViewSet, but can’t get this implementation serving DELETE. Only POST is served properly.

The quesrtion is: what’s wrong in the configuration? Why DRF returns 405 “Method Not Allowed” on DELETE calls and returns Http header Allow: POST, OPTIONS?

Router configuration:

from rest_framework import routers
from api.views import FavoriteViewSet

_v1_router = routers.DefaultRouter()
_v1_router.register(
    r'recipes/(?P<recipe_id>d+)/favorite',
    FavoriteViewSet,
    basename='favorite')

View:

class FavoriteViewSet(
    mixins.CreateModelMixin,
    mixins.DestroyModelMixin,
    viewsets.GenericViewSet):
permission_classes = (IsAuthenticated,)
http_method_names = ('post', 'delete', 'head', 'options',)
queryset = Favorite.objects.all()
serializer_class = FavoriteSerializer
pagination_class = None

def get_permissions(self):
    return super().get_permissions()

def get_object(self):
    return super().get_object()

def retrieve(self, request, *args, **kwargs):
    instance = self.get_object()
    serializer = self.get_serializer(instance)
    return Response(serializer.data)

def destroy(self, request, *args, **kwargs):
    super().destroy(request, *args, **kwargs)

def get_serializer_class(self):
    return super().get_serializer_class()

def dispatch(self, request, *args, **kwargs):
    p1 = hasattr(self, 'get')
    p2 = hasattr(self, 'post')
    p3 = hasattr(self, 'delete')
    p4 = hasattr(self, 'options')
    return super().dispatch(request, *args, **kwargs)

Having spent some time in the Debugger, I figured out, that router configures two instances of my View, but serves reqyuest each time on the first one.

def as_view(cls, actions=None, initkwargs) – got called twice during initialization.
With parameters:

post: create
suffix: List, basename: favorite, detail: False

get: retrieve, delete: destroy
suffix: Instance, basename: favorite, detail: True

I see,that something is wrong with configuration, but couldn’t figure out yet how make DELETE request to the same URL being served on the second instance.

Under the Debugger I see thet in dispatch:

def dispatch(self, request, *args, **kwargs):
    p1 = hasattr(self, 'get')
    p2 = hasattr(self, 'post')
    p3 = hasattr(self, 'delete')
    p4 = hasattr(self, 'options')
    return super().dispatch(request, *args, **kwargs)

On picked view instance only p2 and p4 are True, so, view does not see ‘delete’ attribute presenting in my view and rejects the request with 405.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật