#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 <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/066.3b2b700b0b8b08cc8a59972f16e123c7%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.