Opened 14 months ago

Closed 6 months ago

Last modified 6 months 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 timgraham 6 months ago.

Download all attachments as: .zip

Change History (11)

comment:1 Changed 14 months 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 13 months ago by mjtamlyn

  • Triage Stage changed from Unreviewed to Accepted

comment:3 Changed 13 months ago by mjtamlyn

  • Type changed from Uncategorized to Bug

comment:4 Changed 12 months ago by timo

  • Component changed from Generic views to Documentation
  • Type changed from Bug to Cleanup/optimization

Decided to go the documentation route on the PR.

Changed 6 months ago by timgraham

comment:5 Changed 6 months ago by timgraham

  • Has patch set

comment:6 Changed 6 months ago by claudep

  • Triage Stage changed from Accepted to Ready for checkin

comment:7 Changed 6 months ago by Tim Graham <timograham@…>

  • Resolution set to fixed
  • Status changed from new to closed

In 03c1609c473e14063bcea02dec248095d9c10579:

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

comment:8 Changed 6 months 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 6 months 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 6 months 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