Hi,

While writing tests for views, I discovered the following code and wondered 
whether it is a feature or a potentially bad pattern...

* BaseListView sets self.object_list: 
https://github.com/django/django/blob/ec469ade2b04b94bfeb59fb0fc7d9300470be615/django/views/generic/list.py#L137
* Then BaseListView passes object_list as keyword argument to 
get_context_data(): 
https://github.com/django/django/blob/ec469ade2b04b94bfeb59fb0fc7d9300470be615/django/views/generic/list.py#L152
* But the first thing MultipleObjectMixin.get_context_data() does is 
kwargs.pop('object_list'): 
https://github.com/django/django/blob/ec469ade2b04b94bfeb59fb0fc7d9300470be615/django/views/generic/list.py#L108
* Note: this implementation makes "object_list" argument mandatory for 
get_context_data.

Questions are:

* could MultipleObjectMixin.get_context_data() use self.object_list 
directly? i.e. avoid passing it as kwarg in BaseListView.
* looks like BaseListView.object_list could be some property(get_queryset), 
doesn't it?


Here are notes about the use-case that made me ask... **Am I asking the 
right questions?**

My use case is to write tests for class-based views. I'm testing 
get_context_data() of some custom ListView. Currently I have to pass it 
some arbitrary object_list argument, but I wish the "object_list" context 
data was computed automatically from get_queryset.
Here is a test I wish I could write:

.. code-block:: python::

   class MyModelListViewTestCase(TestCase):
       """Test non-standard behaviour of MyModelListView."""
       def test_context_data(self):
           """get_context_data() contains expected keys/values."""
           instance = MyModel.objects.create()
           request = self.request_factory()  # Uses 
django.test.RequestFactory
           view = self.view_factory(request)  # Populates request, args and 
kwargs attributes, just like dispatch() would.
           data = view.get_context_data(**view.kwargs)  # I also wish I 
could ``data = view.get_context_data()``
           self.assertEqual(data['object_list'], [instance])
           self.assertEqual(data['some_custom_context_var'], 'custom_value')
 
Notice that I use "view.get_context_data(**view.kwargs)" because 
[https://github.com/django/django/blob/ec469ade2b04b94bfeb59fb0fc7d9300470be615/django/views/generic/base.py#L153
 
that's what TemplateView does]... and I really don't understand why 
BaseListView passes "object_list".
In fact, I also wonder why get_context_data() is passed kwargs that are 
already made available as self.kwargs by dispatch...
But I guess I'm missing something ;) ... Any idea?

Regards,

BenoƮt

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to