Opened 11 years ago

Closed 10 years ago

Last modified 10 years ago

#21801 closed Cleanup/optimization (fixed)

SingleObjectMixin get_context_data fails with self.object not initialized

Reported by: anonymous Owned by: nobody
Component: Documentation Version: 1.6
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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.

Attachments (1)

21801.diff (725 bytes ) - added by Tim Graham 10 years ago.

Download all attachments as: .zip

Change History (11)

comment:1 by wraus@…, 11 years ago

Oops, forgot to set the reporter field. Posting here just in case.

comment:2 by Marc Tamlyn, 11 years ago

Triage Stage: UnreviewedAccepted

comment:3 by Marc Tamlyn, 11 years ago

Type: UncategorizedBug

comment:4 by Tim Graham, 11 years ago

Component: Generic viewsDocumentation
Type: BugCleanup/optimization

Decided to go the documentation route on the PR.

by Tim Graham, 10 years ago

Attachment: 21801.diff added

comment:5 by Tim Graham, 10 years ago

Has patch: set

comment:6 by Claude Paroz, 10 years ago

Triage Stage: AcceptedReady for checkin

comment:7 by Tim Graham <timograham@…>, 10 years ago

Resolution: fixed
Status: newclosed

In 03c1609c473e14063bcea02dec248095d9c10579:

Fixed #21801 -- Documented SingleObjectMixin.get_context_data() requires the object attribute.

comment:8 by Tim Graham <timograham@…>, 10 years ago

In 22a28dd8b02a19dc6333b5ab26fec7d6e66c1bf8:

[1.7.x] Fixed #21801 -- Documented SingleObjectMixin.get_context_data() requires the object attribute.

Backport of 03c1609c47 from master

comment:9 by Tim Graham <timograham@…>, 10 years ago

In c0e49ef767e6fd7a4d77ab625e15383948ac250d:

[1.6.x] Fixed #21801 -- Documented SingleObjectMixin.get_context_data() requires the object attribute.

Backport of 03c1609c47 from master

comment:10 by Tim Graham <timograham@…>, 10 years ago

In 03c1609c473e14063bcea02dec248095d9c10579:

Fixed #21801 -- Documented SingleObjectMixin.get_context_data() requires the object attribute.

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