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

Default to default actions of view if .as_view(actions=None) #3742

Closed
niklasfemerstrand opened this issue Dec 15, 2015 · 5 comments
Closed

Comments

@niklasfemerstrand
Copy link

In the latest Django stable one can't use patterns() anymore:

RemovedInDjango110Warning: django.conf.urls.patterns() is deprecated and will be removed in Django 1.10. Update your urlpatterns to be a list of django.conf.urls.url() instances instead.

This was quite nice as one didn't have to pass default actions to views with default actions. When doing the following today:

urlpatterns = [
    url(r"things", views.ThingView.as_view()),
]

We get a warning from the .as_view() override due to the way that #2171 was solved with #2175:

TypeError: The actions argument must be provided when calling .as_view() on a ViewSet. For example .as_view({'get': 'list'})

The problematic part is when ThingView uses e.g. ModelViewSet or another viewset with default actions defined. It would be nice if the patch for #2171 would take the type of view in consideration and if actions is None default to the actions that exist in the viewset by default. It currently appears that we need to solve the problem this way, passing actions dictionaries for a viewset with default actions set because actions is not permitted to be None and does not default to the default values of the view:

urlpatterns = [
    url(r"things/$", views.ThingView.as_view({"get": "list"}), name="ticket-list"),
    url(r"things/(?P<pk>[0-9]+)/$", views.ThingView.as_view({"get": "retrieve", "post": "create", "put": "update", "patch": "partial_update", "delete": "destroy"}), name="ticket-detail"),
]
@xordoquy
Copy link
Collaborator

The problematic part is when ThingView uses e.g. ModelViewSet or another viewset with default actions defined.

What is the problem you are referring to ?

@niklasfemerstrand
Copy link
Author

What is the problem you are referring to ?

That the actions dictionary needs to be passed when using .as_view() on ModelViewSet and ReadOnlyModelViewSet views even though those particular view types have default actions defined.

@niklasfemerstrand
Copy link
Author

Something like this could work in rest_framework/viewsets.py i think:

        # actions must not be empty
        if not actions:
            if cls.__base__ is ModelViewSet:
                # actions = ModelViewSet default actions
            elif cls.__base__ is ReadOnlyModelViewSet:
                # actions = ReadOnlyModelViewSet
            else:
                raise TypeError("The `actions` argument must be provided when "
                                "calling `.as_view()` on a ViewSet. For example "
                                "`.as_view({'get': 'list'})`")

See #2175 for code placement. I have to try some more things that I don't have time for right now before making a pull request.

@xordoquy
Copy link
Collaborator

That the actions dictionary needs to be passed when using .as_view() on ModelViewSet and ReadOnlyModelViewSet views even though those particular view types have default actions defined.

Actually, the two you mentioned should have the actions argument.
One needs to map the get either to list or to retrieve depending on the existence of the PK. Therefore you can't have an empty action.

@shrikantgith
Copy link

thank You so much. You saved my time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants