mari mari - 2 months ago 21
Python Question

Django - Filtering inside of get_context_data

Using class-based views in Django, I'm having a problem filering inside of a DetailView.

What i would like to get is a list of all movies in a specific genre ie:

Movie.objects.all().filter(genre=genre_id)
.

class GenreView(generic.DetailView):
model = Genre
template_name = 'movies/genre.html'
context_object_name = 'this_genre'
def get_context_data(self, **kwargs):
context = super(GenreView, self).get_context_data(**kwargs)
context.update({
'all_movies': Movie.objects.all().filter(genre=pk),
'all_genres': Genre.objects.all()
})
return context


I get this error:

Traceback (most recent call last):
File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\core\handler
s\base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\core\handler
s\base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\views\generi
c\base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\views\generi
c\base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\views\generi
c\detail.py", line 118, in get
context = self.get_context_data(object=self.object)
File "C:\Users\admin\trailers\movies\views.py", line 43, in get_context_data
'all_movies': Movie.objects.all().filter(genre=pk),
NameError: name 'pk' is not defined


When I tried replacing
Movie.objects.all().filter(genre=pk)
with
Movie.objects.all().filter(genre=kwargs['pk'])
I got:

Traceback (most recent call last):
File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\core\handler
s\base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\core\handler
s\base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\views\generi
c\base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\views\generi
c\base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\views\generi
c\detail.py", line 118, in get
context = self.get_context_data(object=self.object)
File "C:\Users\admin\trailers\movies\views.py", line 43, in get_context_data
'all_movies': Movie.objects.all().filter(genre=kwargs['pk']),
KeyError: 'pk'


I can't seem to figure out what's wrong, any help would be appreciated!

Answer

The kwargs parameter passed to get_context_data doesn't contain the primary key value of the object. You can get it from self.kwargs though:

Movie.objects.all().filter(genre=self.kwargs['pk'])

Furthermore, you will see self.object (which is the the Genre instance fetched) is present and accessible when you are inside get_context_data, so you can use that in order to do the following as well:

Movie.objects.all().filter(genre=self.object)