Opened 3 years ago

Closed 2 years ago

Last modified 2 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 2 years ago.

Download all attachments as: .zip

Change History (11)

comment:1 Changed 3 years ago by wraus@…

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset

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

comment:2 Changed 3 years ago by Marc Tamlyn

Triage Stage: UnreviewedAccepted

comment:3 Changed 3 years ago by Marc Tamlyn

Type: UncategorizedBug

comment:4 Changed 3 years ago by Tim Graham

Component: Generic viewsDocumentation
Type: BugCleanup/optimization

Decided to go the documentation route on the PR.

Changed 2 years ago by Tim Graham

Attachment: 21801.diff added

comment:5 Changed 2 years ago by Tim Graham

Has patch: set

comment:6 Changed 2 years ago by Claude Paroz

Triage Stage: AcceptedReady for checkin

comment:7 Changed 2 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: newclosed

In 03c1609c473e14063bcea02dec248095d9c10579:

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

comment:8 Changed 2 years ago by Tim Graham <timograham@…>

In 22a28dd8b02a19dc6333b5ab26fec7d6e66c1bf8:

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

Backport of 03c1609c47 from master

comment:9 Changed 2 years ago by Tim Graham <timograham@…>

In c0e49ef767e6fd7a4d77ab625e15383948ac250d:

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

Backport of 03c1609c47 from master

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

In 03c1609c473e14063bcea02dec248095d9c10579:

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

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