django-rest-framework icon indicating copy to clipboard operation
django-rest-framework copied to clipboard

Seems `Viewset`s `get_extra_action_url_map` method does not recognize namespaces

Open gabn88 opened this issue 3 years ago • 1 comments

Seems Viewsets get_extra_action_url_map method does not recognize namespaces. I'm using different apps via Django URL namespaces (":") but the DRF action pull down menu will not provide it. My quick fix looks like:

    def get_extra_action_url_map(self):
        action_urls = OrderedDict()

        # exit early if `detail` has not been provided
        if self.detail is None:
            return action_urls

        # filter for the relevant extra actions
        actions = [
            action for action in self.get_extra_actions()
            if action.detail == self.detail
        ]
        for action in actions:
            try:
                url_name = '%s-%s' % (self.basename, action.url_name)
                namespace = self.request.resolver_match.namespace
                if namespace:
                    url_name = '%s:%s' % (namespace, url_name)
                url = reverse(url_name, self.args, self.kwargs,
                              request=self.request)
                view = self.__class__(**action.kwargs)
                action_urls[view.get_view_name()] = url
            except NoReverseMatch as exc:
                pass  # URL requires additional arguments, ignore

        return action_urls

which basically add these two lines:

                namespace = self.request.resolver_match.namespace
                if namespace:
                    url_name = '%s:%s' % (namespace, url_name)

The fix works fine for me since years, but it seems that nobody else has been stumbled about it before.

Regards Frank

Originally posted by @backbohne in https://github.com/encode/django-rest-framework/discussions/7816

Is there any specific reason why this fix is not merged?

gabn88 avatar Apr 12 '22 12:04 gabn88

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jun 12 '22 12:06 stale[bot]

Tickets like this mostly get stuck because there's always associated risk that the change will break existing projects.

Personally I think that ViewSets and Routers are an overcomplicated abstraction that we introduced, and it's difficult to pick apart the impact of any changes around them.

If we did want to push forward with a change here, then the first step would be a pull request introducing this change, in order to determine if that breaks any existing tests. That'd at least give us some level of confidence in the change.

lovelydinosaur avatar Aug 10 '22 11:08 lovelydinosaur

@tomchristie Thank you for responding on this issue. I have created the pull request as per your advice. https://github.com/encode/django-rest-framework/pull/8598 I cannot see how this would create problems in existing projects. The only case I can think of is that someone created extra actions and does (for some reason) not want to see them. But IMO you should in that case not use a glitch in DRF, but hide them in an other way (most likely permission based, because hiding still does not prevent them from using the extra action).

gabn88 avatar Aug 10 '22 12:08 gabn88