[Django] #20941: Provide a way for CBVs to be called from instances

14 views
Skip to first unread message

Django

unread,
Aug 19, 2013, 12:39:40 PM8/19/13
to django-...@googlegroups.com
#20941: Provide a way for CBVs to be called from instances
-----------------------------------------+--------------------------
Reporter: mjtamlyn | Owner: mjtamlyn
Type: New feature | Status: new
Component: Generic views | Version:
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+--------------------------
With the changes in the dispatch method to make overriding the method
easier, we lost the ability to easily instantiate and call a class based
view directly. There are some custom use cases where this is useful - and
it's generally useful in tests. The end user perhaps has to be careful
about thread safety, but nonetheless.

Ideally this would be callable after `__init__()` so the view instance can
have some processing done before the request is known.

This would also be useful for providing better ways to test the CBVs.

--
Ticket URL: <https://code.djangoproject.com/ticket/20941>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Aug 23, 2013, 7:16:40 AM8/23/13
to django-...@googlegroups.com
#20941: Provide a way for CBVs to be called from instances
-------------------------------+------------------------------------

Reporter: mjtamlyn | Owner: mjtamlyn
Type: New feature | Status: new
Component: Generic views | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by timo):

* version: => master
* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/20941#comment:1>

Django

unread,
Nov 10, 2014, 5:40:08 AM11/10/14
to django-...@googlegroups.com
#20941: Provide a way for CBVs to be called from instances
-------------------------------+------------------------------------

Reporter: mjtamlyn | Owner: mjtamlyn
Type: New feature | Status: new
Component: Generic views | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------

Comment (by tomchristie):

Okay, so what am I missing here?

{{{
request = self.factory.get('/')
response = MyView.as_view()(request)
}}}

What use case or functionality is this feature request trying to address
that isn't covered by the above?

--
Ticket URL: <https://code.djangoproject.com/ticket/20941#comment:2>

Django

unread,
Nov 10, 2014, 3:44:43 PM11/10/14
to django-...@googlegroups.com
#20941: Provide a way for CBVs to be called from instances
-------------------------------+------------------------------------

Reporter: mjtamlyn | Owner: mjtamlyn
Type: New feature | Status: new
Component: Generic views | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------

Comment (by mjtamlyn):

That's not an instance! `as_view()` gives an opaque function with no
access to the instance of the class, so we can't test parts of it
individually.

The change in the dispatch method I mentioned means that the following
code currently does not work as you might expect (in particular,
`self.args` is not set):


{{{
request = self.factory.get('/')

view = MyView(**initkwargs)
response = view.dispatch(request, *args, **kwargs)
}}}

I acknowledge that this is perhaps not that different from calling
`as_view()`, but having an API along the lines of
`MyView().setup_request(request, *args, **kwargs)` would allow more atomic
testing of class based views in a nicer fashion. Compare below:

{{{
# current
book = Book.objects.create(...)
view = BookDetail()
view.request = request
view.args = ()
view.kwargs = {'pk': book.pk}
self.assertEqual(view.get_object(), book)

# possible better version
book = Book.objects.create(...)
view = BookDetail().setup_request(request, pk=book.pk)
self.assertEqual(view.get_object(), book)
}}}

It's not a big difference, but in my opinion it's a nice one.

--
Ticket URL: <https://code.djangoproject.com/ticket/20941#comment:3>

Django

unread,
Mar 13, 2015, 11:04:59 PM3/13/15
to django-...@googlegroups.com
#20941: Provide a way for CBVs to be called from instances
-------------------------------+------------------------------------

Reporter: mjtamlyn | Owner: mjtamlyn
Type: New feature | Status: new
Component: Generic views | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------

Comment (by collinanderson):

If it helps, you can now introspect the view class and args from the
as_view() function. #24055

--
Ticket URL: <https://code.djangoproject.com/ticket/20941#comment:4>

Django

unread,
Nov 22, 2019, 5:48:21 AM11/22/19
to django-...@googlegroups.com
#20941: Provide a way for CBVs to be called from instances
-------------------------------+---------------------------------------
Reporter: Marc Tamlyn | Owner: Marc Tamlyn
Type: New feature | Status: closed

Component: Generic views | Version: master
Severity: Normal | Resolution: duplicate
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+---------------------------------------
Changes (by Baptiste Mispelon):

* status: new => closed
* resolution: => duplicate


Comment:

#29750 introduced `View.setup()` which seems to be exactly what Marc
proposed in his comment (with the name `setup_request`).

--
Ticket URL: <https://code.djangoproject.com/ticket/20941#comment:5>

Reply all
Reply to author
Forward
0 new messages