Storing values from DetailView.get_object

In my DetailView - I call get_object more than once because I use it in the test_func for UserPassesTestMixin.

Is there a way to prevent firing the query twice? Can I store the value of get_object somehow so that it can be used in the test_func as well as the get_context_data function?

Since you’re using a class based view, a good candidact would be the CBV instance itself self.something = get_object(foo=bar)

get_object is called by get in a DetailView (actually, from the BaseDetailView class).

def get(self, request, *args, **kwargs):
    self.object = self.get_object()
    context = self.get_context_data(object=self.object)
    return self.render_to_response(context)

So as @leandrodesouzadev points out, you can use that first line (self.object = self.get_object()) in your test_func, then override the get method with the above function with the self.object = ... line removed.