Opened 6 years ago

Closed 3 years ago

#15303 closed Cleanup/optimization (needsinfo)

Clarify View.as_view() docs in respect to functions passed as kwargs.

Reported by: Gabriel Grant Owned by: nobody
Component: Documentation Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Problem:

At the moment, generic.base.View.as_view() passes kwargs to the class' init (which setattrs them), rather than dynamically subclassing. The introductory generic view docs, however, seem to imply that passing kwargs to the as_view() method is equivalent to overriding the class. For example, it refers to "pass[ing] the new attributes into the as_view method call" as an "overriding pattern." This makes it rather surprising that functions passed to as_view() aren't bound to the resultant instance.

Possible Resolutions:

It seems to me that as_view() should dynamically create a subclass with the provided kwargs. Alternatively, if that is deemed undesirable, the docs should at least be clarified to better explain the true behaviour.

I can provide a patch, but need someone (*cough* RKM :) to confirm the desired behaviour.

Change History (8)

comment:1 Changed 6 years ago by Russell Keith-Magee

Component: Generic viewsDocumentation
Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Triage Stage: UnreviewedAccepted

As discussed on IRC: I'm not sure I see why we should make the implementation of the generic view base class a whole lot more complicated in order to allow users to avoid creating a subclass. Passing arguments in to as_view() is a convenience for the really simple cases -- it isn't intended as a way to avoid ever needed to create a View subclass.

However, I can see how the phrasing about "overriding pattern" could lead to this expectation, so that should be cleaned up.

comment:2 Changed 6 years ago by Łukasz Rekucki

Severity: Normal
Summary: Functions passed as kwargs to View.as_view() should be boundClarify View.as_view() docs in respect to functions passed as kwargs.
Type: Cleanup/optimization

comment:3 Changed 5 years ago by Jacob

milestone: 1.3

Milestone 1.3 deleted

comment:11 Changed 5 years ago by Aymeric Augustin

UI/UX: unset

Change UI/UX from NULL to False.

comment:12 Changed 5 years ago by Aymeric Augustin

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:13 Changed 4 years ago by Tim Graham

Has patch: set

Not sure if this patch was in response to this ticket, but this looks related at least: https://github.com/django/django/pull/314

comment:14 Changed 4 years ago by Tim Graham

Has patch: unset

I've merged the pull request above, but it seems like there may be additional clarification that should be added (not sure what exactly).

comment:15 Changed 3 years ago by Tim Graham

Resolution: needsinfo
Status: newclosed

@gg, if you can provide a patch please reopen. Otherwise, I'm not sure what further clarification (if any) is needed here.

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