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

AssertionError for OPTIONS call on list-view that accepts PUT #3356

Closed
liquidpele opened this issue Sep 2, 2015 · 1 comment
Closed

AssertionError for OPTIONS call on list-view that accepts PUT #3356

liquidpele opened this issue Sep 2, 2015 · 1 comment
Labels

Comments

@liquidpele
Copy link

We have a viewset something like this:

class RepoViewSet(ReadOnlyModelViewSet):
    queryset = SoftwareRepository.objects.all()
    serializer_class = RepositorySerializer

   @list_route(methods=['GET', 'PUT'])
   def sync(self, request):
        # do stuff

However, in the api browser, if I visit that URL and then click OPTIONS then it throws a 500 error due to AssertionError being thrown by SimpleMetadata object... it seems that it tries get_object() which bombs out because it assumes that there is a pk just from it accepting PUT ?

Traceback:
File "/usr/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response
  112.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/python2.6/site-packages/django/views/decorators/csrf.py" in wrapped_view
  57.         return view_func(*args, **kwargs)
File "/usr/lib/python2.6/site-packages/djangorestframework-3.1.3-py2.6.egg/rest_framework/viewsets.py" in view
  85.             return self.dispatch(request, *args, **kwargs)
File "/usr/lib/python2.6/site-packages/djangorestframework-3.1.3-py2.6.egg/rest_framework/views.py" in dispatch
  456.             response = self.handle_exception(exc)
File "/usr/lib/python2.6/site-packages/djangorestframework-3.1.3-py2.6.egg/rest_framework/views.py" in dispatch
  453.             response = handler(request, *args, **kwargs)
File "/usr/lib/python2.6/site-packages/djangorestframework-3.1.3-py2.6.egg/rest_framework/views.py" in options
  467.         data = self.metadata_class().determine_metadata(request, self)
File "/usr/lib/python2.6/site-packages/djangorestframework-3.1.3-py2.6.egg/rest_framework/metadata.py" in determine_metadata
  64.             actions = self.determine_actions(request, view)
File "/usr/lib/python2.6/site-packages/djangorestframework-3.1.3-py2.6.egg/rest_framework/metadata.py" in determine_actions
  83.                     view.get_object()
File "/usr/lib/python2.6/site-packages/djangorestframework-3.1.3-py2.6.egg/rest_framework/generics.py" in get_object
  91.             (self.__class__.__name__, lookup_url_kwarg)

Exception Type: AssertionError at /rest/v1/repositories/sync
Exception Value: Expected view RepoViewSet to be called with a URL keyword argument named "pk". Fix your URL conf, or set the `.lookup_field` attribute on the view correctly.
@xordoquy
Copy link
Collaborator

OK, I initially though this was a bug but after a closer look at it I don't think so.

PUT are supposed to be done on a resource:

The PUT method requests that the enclosed entity be stored under the supplied Request-URI. If the Request-URI refers to an already existing resource, the enclosed entity SHOULD be considered as a modified version of the one residing on the origin server. If the Request-URI does not point to an existing resource, and that URI is capable of being defined as a new resource by the requesting user agent, the origin server can create the resource with that URI.

I do consider the list_route decorator doesn't point to an existing resource nor that the URI is capable of being defined as a new resource since it's a list.
My advice would be to override the default meta.

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

No branches or pull requests

2 participants