Skip to content

OPTIONS method on a listview calls get_object() which fails an assertion #3994

@stiffupperlip

Description

@stiffupperlip

When I send OPTIONS request to this view, it tries to call generics.GenericAPIView.get_object() method which raises assertion error, because there is no pk argument in url pattern, which is not needed because this is a list view.

URL pattern:

url(r'api/services/?$', views.ServiceView.as_view()),
url(r'api/services/(?P<pk>\d+)/?$', views.ServiceDetailView.as_view()), # this is another view that `OPTIONS` work fine for.

View class:

class ServiceView(generics.GenericAPIView):
    queryset = models.Service.objects.all()
    serializer_class = serializers.ServiceSerializer
    search_fields = ('name',)
    filter_fields = ('id', 'name')
    ordering_fields = '__all__'
    ordering = ('id',)

When I send this request:
http --auth username:password options localhost:8000/api/services

Here's the traceback:

api.views ERROR Exception "AssertionError": "Expected view ServiceView to be called with a URL keyword argument named "pk". Fix your URL conf, or set the `.lookup_field` attribute on the view correctly."
Traceback (most recent call last):
  File "/home/a/.virtualenvs/ch/local/lib/python2.7/site-packages/rest_framework/views.py", line 463, in dispatch
response = handler(request, *args, **kwargs)
  File "/home/a/.virtualenvs/ch/local/lib/python2.7/site-packages/rest_framework/views.py", line 477, in options
data = self.metadata_class().determine_metadata(request, self)
  File "/home/a/.virtualenvs/ch/local/lib/python2.7/site-packages/rest_framework/metadata.py", line 69, in determine_metadata
actions = self.determine_actions(request, view)
  File "/home/a/.virtualenvs/ch/local/lib/python2.7/site-packages/rest_framework/metadata.py", line 88, in determine_actions
view.get_object()
  File "/home/a/.virtualenvs/ch/local/lib/python2.7/site-packages/rest_framework/generics.py", line 93, in get_object
(self.__class__.__name__, lookup_url_kwarg)
AssertionError: Expected view ServiceView to be called with a URL keyword argument named "pk". Fix your URL conf, or set the `.lookup_field` attribute on the view correctly.
[14/Mar/2016 17:30:21] "OPTIONS /api/services HTTP/1.1" 500 33

OPTIONS method was used to work when I subclassed APIView for views, but now that I'm subclassing generics.GenericAPIView, It fails.

Django REST framework 3.3.3, Django 1.8.11, Python 2.7.6, Ubuntu 14.04

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions