Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #1083 from Markush2010/ticket20235

Fixes #20235 - MultipleObjectMixin requires object_list in kwargs
  • Loading branch information...
commit ef73a8eefd2c4836ca2badc67c3b9aa9a0add7bd 2 parents be826aa + 1c921cf
Marc Tamlyn authored May 18, 2013
1  AUTHORS
@@ -274,6 +274,7 @@ answer newbie questions, and generally made Django that much better:
274 274
     Eric Holscher <http://ericholscher.com>
275 275
     Ian Holsman <http://feh.holsman.net/>
276 276
     Kieran Holland <http://www.kieranholland.com>
  277
+    Markus Holtermann <http://markusholtermann.eu>
277 278
     Sung-Jin Hong <serialx.net@gmail.com>
278 279
     Leo "hylje" Honkanen <sealage@gmail.com>
279 280
     Matt Hoskins <skaffenuk@googlemail.com>
4  django/views/generic/list.py
@@ -105,7 +105,7 @@ def get_context_data(self, **kwargs):
105 105
         """
106 106
         Get the context for this view.
107 107
         """
108  
-        queryset = kwargs.pop('object_list')
  108
+        queryset = kwargs.pop('object_list', self.object_list)
109 109
         page_size = self.get_paginate_by(queryset)
110 110
         context_object_name = self.get_context_object_name(queryset)
111 111
         if page_size:
@@ -149,7 +149,7 @@ def get(self, request, *args, **kwargs):
149 149
             if is_empty:
150 150
                 raise Http404(_("Empty list and '%(class_name)s.allow_empty' is False.")
151 151
                         % {'class_name': self.__class__.__name__})
152  
-        context = self.get_context_data(object_list=self.object_list)
  152
+        context = self.get_context_data()
153 153
         return self.render_to_response(context)
154 154
 
155 155
 
20  tests/generic_views/test_base.py
@@ -411,3 +411,23 @@ def test_get_context_data_super(self):
411 411
         # test that kwarg overrides values assigned higher up
412 412
         context = test_view.get_context_data(test_name='test_value')
413 413
         self.assertEqual(context['test_name'], 'test_value')
  414
+
  415
+
  416
+class UseMultipleObjectMixinTest(unittest.TestCase):
  417
+    rf = RequestFactory()
  418
+
  419
+    def test_use_queryset_from_view(self):
  420
+        test_view = views.CustomMultipleObjectMixinView()
  421
+        test_view.get(self.rf.get('/'))
  422
+        # Don't pass queryset as argument
  423
+        context = test_view.get_context_data()
  424
+        self.assertEqual(context['object_list'], test_view.queryset)
  425
+
  426
+    def test_overwrite_queryset(self):
  427
+        test_view = views.CustomMultipleObjectMixinView()
  428
+        test_view.get(self.rf.get('/'))
  429
+        queryset = [{'name': 'Lennon'}, {'name': 'Ono'}]
  430
+        self.assertNotEqual(test_view.queryset, queryset)
  431
+        # Overwrite the view's queryset with queryset from kwarg
  432
+        context = test_view.get_context_data(object_list=queryset)
  433
+        self.assertEqual(context['object_list'], queryset)
11  tests/generic_views/views.py
@@ -201,6 +201,17 @@ def get_object(self, queryset=None):
201 201
         return super(BookDetailGetObjectCustomQueryset,self).get_object(
202 202
             queryset=Book.objects.filter(pk=2))
203 203
 
  204
+
  205
+class CustomMultipleObjectMixinView(generic.list.MultipleObjectMixin, generic.View):
  206
+    queryset = [
  207
+        {'name': 'John'},
  208
+        {'name': 'Yoko'},
  209
+    ]
  210
+
  211
+    def get(self, request):
  212
+        self.object_list = self.get_queryset()
  213
+
  214
+
204 215
 class CustomContextView(generic.detail.SingleObjectMixin, generic.View):
205 216
     model = Book
206 217
     object = Book(name='dummy')

0 notes on commit ef73a8e

Please sign in to comment.
Something went wrong with that request. Please try again.