Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Easier way to set view name #5755

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/api-guide/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ This should be a function with the following signature:

view_name(cls, suffix=None)

* `cls`: The view class. Typically the name function would inspect the name of the class when generating a descriptive name, by accessing `cls.__name__`.
* `cls`: The view class. Typically the name function would inspect the name of the class when generating a descriptive name, by accessing `cls.__name__`. You can set a different name by setting `view_name` variable in your view.
* `suffix`: The optional suffix used when differentiating individual views in a viewset.

Default: `'rest_framework.views.get_view_name'`
Expand Down
2 changes: 1 addition & 1 deletion rest_framework/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def get_view_name(view_cls, suffix=None):

This function is the default for the `VIEW_NAME_FUNCTION` setting.
"""
name = view_cls.__name__
name = view_cls.view_name if hasattr(view_cls, 'view_name') else view_cls.__name__
name = formatting.remove_trailing_string(name, 'View')
name = formatting.remove_trailing_string(name, 'ViewSet')
name = formatting.camelcase_to_spaces(name)
Expand Down
13 changes: 13 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def get_view_name(self):
return "Foo"


class CustomNameByVariableResourceInstance(APIView):
view_name = "Bar"


class ResourceViewSet(ModelViewSet):
serializer_class = ModelSerializer
queryset = BasicModel.objects.all()
Expand All @@ -50,6 +54,7 @@ class ResourceViewSet(ModelViewSet):
url(r'^$', Root.as_view()),
url(r'^resource/$', ResourceRoot.as_view()),
url(r'^resource/customname$', CustomNameResourceInstance.as_view()),
url(r'^resource/customnamebyvar$', CustomNameByVariableResourceInstance.as_view()),
url(r'^resource/(?P<key>[0-9]+)$', ResourceInstance.as_view()),
url(r'^resource/(?P<key>[0-9]+)/$', NestedResourceRoot.as_view()),
url(r'^resource/(?P<key>[0-9]+)/(?P<other>[A-Za-z]+)$', NestedResourceInstance.as_view()),
Expand Down Expand Up @@ -88,6 +93,14 @@ def test_resource_instance_customname_breadcrumbs(self):
('Foo', '/resource/customname')
]

def test_resource_instance_customnamebyvar_breadcrumbs(self):
url = '/resource/customnamebyvar'
assert get_breadcrumbs(url) == [
('Root', '/'),
('Resource Root', '/resource/'),
('Bar', '/resource/customnamebyvar')
]

def test_nested_resource_breadcrumbs(self):
url = '/resource/123/'
assert get_breadcrumbs(url) == [
Expand Down