From 1c921cfac34daa9c18e4e235e20fa7beb8929f1c Mon Sep 17 00:00:00 2001 From: Matthew Somerville Date: Wed, 10 Apr 2013 11:27:28 +0100 Subject: [PATCH] Fixed #20235 -- Use self.object_list if object_list not present in get_context_data kwargs. This is so MultipleObjectMixin can be used in the same way as SingleObjectMixin. --- AUTHORS | 1 + django/views/generic/list.py | 4 ++-- tests/generic_views/test_base.py | 20 ++++++++++++++++++++ tests/generic_views/views.py | 11 +++++++++++ 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/AUTHORS b/AUTHORS index 3544e3b81ac..70f8bb22d90 100644 --- a/AUTHORS +++ b/AUTHORS @@ -274,6 +274,7 @@ answer newbie questions, and generally made Django that much better: Eric Holscher Ian Holsman Kieran Holland + Markus Holtermann Sung-Jin Hong Leo "hylje" Honkanen Matt Hoskins diff --git a/django/views/generic/list.py b/django/views/generic/list.py index 08c4bbcda08..1aff3454f4f 100644 --- a/django/views/generic/list.py +++ b/django/views/generic/list.py @@ -105,7 +105,7 @@ class MultipleObjectMixin(ContextMixin): """ Get the context for this view. """ - queryset = kwargs.pop('object_list') + queryset = kwargs.pop('object_list', self.object_list) page_size = self.get_paginate_by(queryset) context_object_name = self.get_context_object_name(queryset) if page_size: @@ -149,7 +149,7 @@ class BaseListView(MultipleObjectMixin, View): if is_empty: raise Http404(_("Empty list and '%(class_name)s.allow_empty' is False.") % {'class_name': self.__class__.__name__}) - context = self.get_context_data(object_list=self.object_list) + context = self.get_context_data() return self.render_to_response(context) diff --git a/tests/generic_views/test_base.py b/tests/generic_views/test_base.py index 0e84e171324..2eadee2b420 100644 --- a/tests/generic_views/test_base.py +++ b/tests/generic_views/test_base.py @@ -411,3 +411,23 @@ class GetContextDataTest(unittest.TestCase): # test that kwarg overrides values assigned higher up context = test_view.get_context_data(test_name='test_value') self.assertEqual(context['test_name'], 'test_value') + + +class UseMultipleObjectMixinTest(unittest.TestCase): + rf = RequestFactory() + + def test_use_queryset_from_view(self): + test_view = views.CustomMultipleObjectMixinView() + test_view.get(self.rf.get('/')) + # Don't pass queryset as argument + context = test_view.get_context_data() + self.assertEqual(context['object_list'], test_view.queryset) + + def test_overwrite_queryset(self): + test_view = views.CustomMultipleObjectMixinView() + test_view.get(self.rf.get('/')) + queryset = [{'name': 'Lennon'}, {'name': 'Ono'}] + self.assertNotEqual(test_view.queryset, queryset) + # Overwrite the view's queryset with queryset from kwarg + context = test_view.get_context_data(object_list=queryset) + self.assertEqual(context['object_list'], queryset) diff --git a/tests/generic_views/views.py b/tests/generic_views/views.py index aa8777e8c6d..4dda3fe0e0a 100644 --- a/tests/generic_views/views.py +++ b/tests/generic_views/views.py @@ -201,6 +201,17 @@ class BookDetailGetObjectCustomQueryset(BookDetail): return super(BookDetailGetObjectCustomQueryset,self).get_object( queryset=Book.objects.filter(pk=2)) + +class CustomMultipleObjectMixinView(generic.list.MultipleObjectMixin, generic.View): + queryset = [ + {'name': 'John'}, + {'name': 'Yoko'}, + ] + + def get(self, request): + self.object_list = self.get_queryset() + + class CustomContextView(generic.detail.SingleObjectMixin, generic.View): model = Book object = Book(name='dummy')