Fixed #24689 -- Fixed DetailView methods with deferred QuerySet.
This commit is contained in:
parent
0f2e82b9ec
commit
86aaffa5a3
|
@ -89,6 +89,8 @@ class SingleObjectMixin(ContextMixin):
|
||||||
if self.context_object_name:
|
if self.context_object_name:
|
||||||
return self.context_object_name
|
return self.context_object_name
|
||||||
elif isinstance(obj, models.Model):
|
elif isinstance(obj, models.Model):
|
||||||
|
if self.object._deferred:
|
||||||
|
obj = obj._meta.proxy_for_model
|
||||||
return obj._meta.model_name
|
return obj._meta.model_name
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
@ -149,9 +151,12 @@ class SingleObjectTemplateResponseMixin(TemplateResponseMixin):
|
||||||
# The least-specific option is the default <app>/<model>_detail.html;
|
# The least-specific option is the default <app>/<model>_detail.html;
|
||||||
# only use this if the object in question is a model.
|
# only use this if the object in question is a model.
|
||||||
if isinstance(self.object, models.Model):
|
if isinstance(self.object, models.Model):
|
||||||
|
object_meta = self.object._meta
|
||||||
|
if self.object._deferred:
|
||||||
|
object_meta = self.object._meta.proxy_for_model._meta
|
||||||
names.append("%s/%s%s.html" % (
|
names.append("%s/%s%s.html" % (
|
||||||
self.object._meta.app_label,
|
object_meta.app_label,
|
||||||
self.object._meta.model_name,
|
object_meta.model_name,
|
||||||
self.template_name_suffix
|
self.template_name_suffix
|
||||||
))
|
))
|
||||||
elif hasattr(self, 'model') and self.model is not None and issubclass(self.model, models.Model):
|
elif hasattr(self, 'model') and self.model is not None and issubclass(self.model, models.Model):
|
||||||
|
|
|
@ -5,7 +5,10 @@ import datetime
|
||||||
|
|
||||||
from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist
|
from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist
|
||||||
from django.test import TestCase, override_settings
|
from django.test import TestCase, override_settings
|
||||||
|
from django.test.client import RequestFactory
|
||||||
from django.views.generic.base import View
|
from django.views.generic.base import View
|
||||||
|
from django.views.generic.detail import SingleObjectTemplateResponseMixin
|
||||||
|
from django.views.generic.edit import ModelFormMixin
|
||||||
|
|
||||||
from .models import Artist, Author, Book, Page
|
from .models import Artist, Author, Book, Page
|
||||||
|
|
||||||
|
@ -137,6 +140,25 @@ class DetailViewTest(TestCase):
|
||||||
self.assertNotIn('author', res.context)
|
self.assertNotIn('author', res.context)
|
||||||
self.assertTemplateUsed(res, 'generic_views/author_detail.html')
|
self.assertTemplateUsed(res, 'generic_views/author_detail.html')
|
||||||
|
|
||||||
|
def test_deferred_queryset_template_name(self):
|
||||||
|
class FormContext(SingleObjectTemplateResponseMixin):
|
||||||
|
request = RequestFactory().get('/')
|
||||||
|
model = Author
|
||||||
|
object = Author.objects.defer('name').get(pk=self.author1.pk)
|
||||||
|
|
||||||
|
self.assertEqual(FormContext().get_template_names()[0], 'generic_views/author_detail.html')
|
||||||
|
|
||||||
|
def test_deferred_queryset_context_object_name(self):
|
||||||
|
class FormContext(ModelFormMixin):
|
||||||
|
request = RequestFactory().get('/')
|
||||||
|
model = Author
|
||||||
|
object = Author.objects.defer('name').get(pk=self.author1.pk)
|
||||||
|
fields = ('name',)
|
||||||
|
|
||||||
|
form_context_data = FormContext().get_context_data()
|
||||||
|
self.assertEqual(form_context_data['object'], self.author1)
|
||||||
|
self.assertEqual(form_context_data['author'], self.author1)
|
||||||
|
|
||||||
def test_invalid_url(self):
|
def test_invalid_url(self):
|
||||||
self.assertRaises(AttributeError, self.client.get, '/detail/author/invalid/url/')
|
self.assertRaises(AttributeError, self.client.get, '/detail/author/invalid/url/')
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue