Opened 3 years ago

Closed 3 years ago

#25235 closed Cleanup/optimization (wontfix)

Document alternate syntax for class-based view decoration

Reported by: zauddelig Owned by: nobody
Component: Utilities Version: 1.8
Severity: Normal Keywords: documentation, class based views, utilities, decorators
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

The class based views decoration documentation is in my opinion ugly and I think is keeping some beginners from class based views.

Based on that ,one has three methods:

  1. Use Multi Inheritance, which is both risky and dreadful, imho is too easy to have side effects, still is the more clean solution described.
  1. Use method_decorator, which is better if not that you have to wrap every decorator function inside of it and are forced rewrite dispatch.
  1. wrapping directly the Klass.as_view(), better but as documented it seem more a problem than anything.

The advantage of the third method is that you preserve the logic of the Classes, so that the same View can change, more or less slightly, it's logic in different contexts.

I usually use solutions like this:

 view = reduce(
    lambda x, f: f(x),
    (
        third_decorator,
        second_decorator,
        first_decorator,
    ),
    ViewKlass.as_view()
)

The good thing is that you can wrap group of common decorators in a single iterable.

I don't know if it would be better to have a django.utils to do this or just add to documentation this or some similar method.

Change History (2)

comment:1 Changed 3 years ago by Tim Graham

django-developers discussion. (For future reference, it's usually better to wait until the mailing list thread reaches a conclusion of what should be done before opening a ticket.)

comment:2 Changed 3 years ago by Tim Graham

Resolution: wontfix
Status: newclosed
Summary: Class based view decorationDocument alternate syntax for class-based view decoration

Closing as #25146 seems to address this concern as discussed on the mailing list.

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