﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
21801	SingleObjectMixin get_context_data fails with self.object not initialized	anonymous	nobody	"I'm not sure if this needs to be addressed in the documentation or in the code, but there's a bit of a gotcha in the SingleObjectMixin code, related to the implementation of get_context_data().

The get_context_data() of SingleObjectMixin looks for self.object to attach the retrieved object to the view. However, the object attribute is not actually set anywhere in the SingleObjectMixin. A little digging says that BaseDetailView addresses this by manually setting self.object to self.get_object() in the get() function. However, that mandates the semi-redundant setting of an attribute that may not be used elsewhere, and also requires one of dispatch, get, or post to be defined. None of this is mentioned in the documentation, though after going back I noticed that there's an unexplained self.object = self.get_object() in the documentation for using mixins.

There are a couple potential ways to deal with this, but I think the simplest way is probably this:

{{{
def get_context_data(self, **kwargs):
    """"""
    Insert the single object into the context dict.
    """"""
    context = {}
    try:
        obj = self.object
    except AttributeError:
        obj = self.get_object()
    if obj:
        context['object'] = obj
        context_object_name = self.get_context_object_name(obj)
        if context_object_name:
            context[context_object_name] = obj
    context.update(kwargs)
    return super(SingleObjectMixin, self).get_context_data(**context)
}}}

I suspect it won't break any existing code, and it'll allow SingleObjectMixin to be used without having to redefine on of the functions that comes before get_context_data() in the chain."	Cleanup/optimization	closed	Documentation	1.6	Normal	fixed			Ready for checkin	1	0	0	0	0	0
