We are updating the django-rest-framework from 3.1.3 to 3.5.3. After updating all of our ModelViewSet and viewsets.GenericViewSet models that use DefaultRouter to generate URLs, they no longer allow HEAD method calls. I looked through the release notes and documents and could not find any settings or changes due to which HEAD was no longer allowed.
I can solve this problem by doing a subclass of DefaultRouter and changing the default settings, but I don't think this is the best or the right solution. From reading as part of the django-rest-framework problems and documentation, it seems that django-rest-framework should automatically handle the HEAD and OPTIONS methods.
@detail_route, @list_route, and views derived from ApiView that allow the GET method to automatically dial the HEAD and OPTION methods.
Why did the HEAD method disappear after this update, and what is the correct method for providing HEAD methods on our routes?
The definitions of our route and ModelViewSet are very standard, here is a non-working route:
from rest_framework.routers import DefaultRouter
from user_profile import views
router = DefaultRouter(trailing_slash=False)
router.register(r'user_names', views.UserNameView)
urlpatterns = router.urls
And the view:
class UserNameView(mixins.ListModelMixin,
mixins.RetrieveModelMixin,
viewsets.GenericViewSet):
queryset = User.objects.only(
"id", "first_name", "last_name", "email",
"mobile_phone", "photo", "is_active", "date_joined"
).select_related("photo").all()
serializer_class = serializers.UserNameSerializer
Postman Response to HEAD Call:
Status: 405 Method Not Allowed
Allow →GET, OPTIONS
Content-Type →application/json
Date →Wed, 09 Nov 2016 20:50:41 GMT
Server →WSGIServer/0.1 Python/2.7.12
Vary →Cookie
X-Frame-Options →SAMEORIGIN
x-xss-protection →1; mode=block