[1.9.x] Fixed #26006 -- Fixed incorrect object reference in SingleObjectMixin.get_context_object_name().

Backport of 4b2dcfe04f from master
This commit is contained in:
Chris Cogdon 2015-12-29 10:58:43 -08:00 committed by Tim Graham
parent 76a96215b9
commit 4d9b98616f
5 changed files with 30 additions and 1 deletions

View File

@ -89,7 +89,7 @@ class SingleObjectMixin(ContextMixin):
if self.context_object_name:
return self.context_object_name
elif isinstance(obj, models.Model):
if self.object._deferred:
if obj._deferred:
obj = obj._meta.proxy_for_model
return obj._meta.model_name
else:

View File

@ -70,3 +70,6 @@ Bugfixes
* Fixed a regression in the admin which ignored line breaks in read-only fields
instead of converting them to ``<br>`` (:ticket:`25465`).
* Fixed incorrect object reference in
``SingleObjectMixin.get_context_object_name()`` (:ticket:`26006`).

View File

@ -140,6 +140,19 @@ class DetailViewTest(TestCase):
self.assertNotIn('author', res.context)
self.assertTemplateUsed(res, 'generic_views/author_detail.html')
def test_custom_detail(self):
"""
AuthorCustomDetail overrides get() and ensures that
SingleObjectMixin.get_context_object_name() always uses the obj
parameter instead of self.object.
"""
res = self.client.get('/detail/author/%s/custom_detail/' % self.author1.pk)
self.assertEqual(res.status_code, 200)
self.assertEqual(res.context['custom_author'], self.author1)
self.assertNotIn('author', res.context)
self.assertNotIn('object', res.context)
self.assertTemplateUsed(res, 'generic_views/author_detail.html')
def test_deferred_queryset_template_name(self):
class FormContext(SingleObjectTemplateResponseMixin):
request = RequestFactory().get('/')

View File

@ -50,6 +50,8 @@ urlpatterns = [
views.AuthorDetail.as_view(template_name='generic_views/about.html')),
url(r'^detail/author/(?P<pk>[0-9]+)/context_object_name/$',
views.AuthorDetail.as_view(context_object_name='thingy')),
url(r'^detail/author/(?P<pk>[0-9]+)/custom_detail/$',
views.AuthorCustomDetail.as_view()),
url(r'^detail/author/(?P<pk>[0-9]+)/dupe_context_object_name/$',
views.AuthorDetail.as_view(context_object_name='object')),
url(r'^detail/page/(?P<pk>[0-9]+)/field/$',

View File

@ -34,6 +34,17 @@ class AuthorDetail(generic.DetailView):
queryset = Author.objects.all()
class AuthorCustomDetail(generic.DetailView):
template_name = 'generic_views/author_detail.html'
queryset = Author.objects.all()
def get(self, request, *args, **kwargs):
# Ensures get_context_object_name() doesn't reference self.object.
author = self.get_object()
context = {'custom_' + self.get_context_object_name(author): author}
return self.render_to_response(context)
class PageDetail(generic.DetailView):
queryset = Page.objects.all()
template_name_field = 'template'