Skip to content

self.action is set to None during HEAD request #6196

Closed

Description

@OskarPersson

Checklist

  • I have verified that that issue exists against the master branch of Django REST framework.
  • I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
  • This is not a usage question. (Those should be directed to the discussion group instead.)
  • This cannot be dealt with as a third party library. (We prefer new functionality to be in the form of third party libraries where possible.)
  • I have reduced the issue to the simplest possible case.

Steps to reproduce

Send a HEAD request to a viewset which prints self.action in get_queryset:

class MyViewSet(viewsets.ModelViewSet):
    def get_queryset(self):
        print self.action

Expected behavior

During a HEAD request this should, just like during GET request, print list or retrieve depending on the route requested.

Actual behavior

None is printed

Activity

  1. xordoquy commented on Sep 19, 2018

    @xordoquy
    Contributor

    This is a side effect of:

    1. default head being set to get
    2. routers not mapping head

    The self.action mapping is performed here

  2. OskarPersson commented on Sep 19, 2018

    @OskarPersson
    ContributorAuthor

    One fix seems to be to just add 'head' with the same value as 'get' to the mappings in SimpleRouter: https://github.com/encode/django-rest-framework/blob/master/rest_framework/routers.py#L123.

    Not sure how it should be tested and if it creates some other problems though.

  3. xordoquy commented on Sep 19, 2018

    @xordoquy
    Contributor

    indeed. I don't see any side effect and it doesn't look like the mapping is tested.
    We'll need to add a line about it in the release note though.

  4. Sinkler commented on Apr 3, 2019

    @Sinkler

    One fix seems to be to just add 'head' with the same value as 'get' to the mappings in SimpleRouter: https://github.com/encode/django-rest-framework/blob/master/rest_framework/routers.py#L123.

    One more action is needed to fix it for dynamic (detail) routes: add head here or here something like this:

        if 'get' in action.mapping and 'head' not in action.mapping:
            action.mapping['head'] = action.mapping['get']
  5. self-assigned this
    on Jul 1, 2019
  6. Bitblade commented on Mar 7, 2020

    @Bitblade

    It doesn't seem this was implemented yet, so I made these workarounds.

    class PatchedDefaultRouter(routers.DefaultRouter):
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
            for route in self.routes:
                if hasattr(route, 'mapping') and 'get' in route.mapping and 'head' not in route.mapping:
                    route.mapping['head'] = route.mapping['get']
    
        def _get_dynamic_route(self, route, action):
            result = super()._get_dynamic_route(route, action)
            if 'get' in result.mapping and 'head' not in result.mapping:
                result.mapping['head'] = result.mapping['get']
            return result
    
    
    class PatchedSimpleRouter(routers.SimpleRouter):
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
            for route in self.routes:
                if hasattr(route, 'mapping'):
                    route.mapping['head'] = 'retrieve' if route.detail else 'list'
        
        def _get_dynamic_route(self, route, action):
            result = super()._get_dynamic_route(route, action)
            if 'get' in result.mapping and 'head' not in result.mapping:
                result.mapping['head'] = result.mapping['get']
            return result
  7. modified the milestone: 3.12 Release on Mar 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions