Code

Opened 3 years ago

Closed 3 years ago

#16931 closed New feature (duplicate)

"get_context_data" should follow MRO

Reported by: linovia Owned by: nobody
Component: Generic views Version: 1.3
Severity: Normal Keywords:
Cc: linovia Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Alex)

Actually, mixins do not allow to cumulate the context building.

For example:

class CommentMixin(FormMixin, SingleObjectMixin):
    pass

In that case, we expect both FormMixin and SingleObjectMixin to build their context.
However, as none of them use super in their get_context_data do use super, we only get the FormMixin context.

The reason to use super here is not to call the parent get_context_data but the next class in MRO.

class A(object):
    def a(self):
        print "A"
        if hasattr(super(A, self), 'a'):
            super(A, self).a()


class B(object):
    def a(self):
        print "B"
        if hasattr(super(B, self), 'a'):
            super(B, self).a()


class C(object):
    def a(self):
        print "C"


class D(B, A):
    pass


class E(C, A):
    pass


print "MRO:", [x.__name__ for x in D.__mro__]
d = D()
d.a()

print "MRO:", [x.__name__ for x in E.__mro__]
e = E()
e.a()

With that example, one can see that d calls both B then A while e only calls C.

While this might usually be unnoticed, it prevents Mixin to be chained.
For example, mixing a single object item with a list (for a shop: one category with the associated products, for a bug tracker: a milestone with the associated tickets) or a single item with a form such as a post with a comment form.

Attachments (0)

Change History (3)

comment:1 Changed 3 years ago by Alex

  • Description modified (diff)
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 3 years ago by linovia

  • Cc linovia added

comment:3 Changed 3 years ago by ptone

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

This turns out to be a dupe of #16074

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.