Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#20235 closed Cleanup/optimization (fixed)

MultipleObjectMixin requires object_list to be passed in rather than using self.object_list

Reported by: Matthew Somerville Owned by: Markus Holtermann
Component: Generic views Version: master
Severity: Normal Keywords:
Cc: tinodb Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no


Unlike SingleObjectMixin, which uses self.object in its get_context_data, MultipleObjectMixin requires that object_list is passed in to get_context_data as a kwarg. BaseListView does this, but using MultipleObjectMixin separately requires this knowledge, not documented on the mixin page at . It is alluded to at but I don't think this is clear, and I think having it in MultipleObjectMixin is a better location, as with SingleObjectMixin.

As I assume MultipleObjectMixin is in use already with its current behaviour, I believe the nicest fix is to add a default to the pop() so that MultipleObjectMixin can be used by setting self.object_list in your subclass, the same as with SingleObjectMixin and self.object.

I have a patch on a branch of my github fork:

Change History (8)

comment:1 Changed 5 years ago by Simon Charette

Triage Stage: UnreviewedAccepted

Was also bitten by this inconsistency. From a quick review the patch seems correct.

comment:2 Changed 5 years ago by tinodb

Cc: tinodb added
Component: UncategorizedGeneric views
Type: UncategorizedCleanup/optimization

A question about the tests. Why are you testing that get_context_object_name works, and not that the correct queryset is used? The first two assertions would also pass without your changes, right? Or do I miss something?

comment:3 Changed 5 years ago by Matthew Somerville

The second test (of object_list) would not pass without the change, because get_context_data() is called without arguments, and without the change that would throw an error as object_list wouldn't be present for the pop. The other tests were just to try and match the SingleObjectMixin test as much as possible.

comment:4 Changed 5 years ago by Markus Holtermann

Owner: changed from nobody to Markus Holtermann
Status: newassigned

comment:5 Changed 5 years ago by Markus Holtermann

The test look fine to me. I rebased your patch onto the current master so that it merges cleanly and opened a pull request at

Thanks for your work, dracos.

comment:6 Changed 5 years ago by Marc Tamlyn

Patch needs improvement: set

As discussed at the sprint, the I share some confusion about what the tests are doing. In particular the customer context_object_name is not necessary, and also setting object_list as a class level variable is ugly.

comment:7 Changed 5 years ago by Markus Holtermann <info@…>

Resolution: fixed
Status: assignedclosed

In 1c921cfac34daa9c18e4e235e20fa7beb8929f1c:

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

comment:8 Changed 5 years ago by Marc Tamlyn <marc.tamlyn@…>

In ef73a8eefd2c4836ca2badc67c3b9aa9a0add7bd:

Merge pull request #1083 from Markush2010/ticket20235

Fixes #20235 - MultipleObjectMixin requires object_list in kwargs

Note: See TracTickets for help on using tickets.
Back to Top