D_D
D_D

Reputation: 383

Getting TypeError using get_queryset() in my Class Based Generic Views

The error says :

get() takes exactly 2 arguments (3 given)

I am sub classing DetailView as :

class MovieDetail(DetailView):
    template_name = 'examples/generic_movie_detail.html'
    template_object_name = 'movie'

    def get_queryset(self):
        movie = get_object_or_404(Movies, actors__name__contains = self.args[0])
        return movie 

and using url as :

(r'^movie/detail/(\w+)', MovieDetail.as_view())

Now i want to retrieve movies by any particular actor say ('Tom') then i will pass Tom through url as "/movie/detail/tom " . I will work it out in my template then .

Traceback :

File "C:\Python26\lib\site-packages\django\core\handlers\base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "C:\Python26\lib\site-packages\django\views\generic\base.py" in view
  47.             return self.dispatch(request, *args, **kwargs)
File "C:\Python26\lib\site-packages\django\views\generic\base.py" in dispatch
  68.         return handler(request, *args, **kwargs)

Exception Type: TypeError at /examples/movie/detail/aamir Exception Value: get() takes exactly 2 arguments (3 given)

Upvotes: 0

Views: 1446

Answers (2)

Udi
Udi

Reputation: 30502

If you are subclassing DetailView, try overriding get_object and not get_queryset.

Edit: Yuji Tomita's answer below seems the right answer for your question, you should probably use probably use self.kwargs['name'] and (r'^movie/detail/(?P<name>\'+)/$', MovieDetail.as_view())

Keep in mind that for a detail view you probably would want to receive exactly one result for a url, and you should use an unique identity field and an "exact" filter instead of a using a foreign key lookup and use "contains".

Upvotes: 0

You are passing BaseDetailView.get arguments via your URL capturing group (the (\w+)) when it doesn't accept arguments.

class BaseDetailView(SingleObjectMixin, View):
    def get(self, request, **kwargs): #<-- only keyword args

Either pass it keyword arguments or override the get method to accept *args or your specific argument.

Upvotes: 1

Related Questions