Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #16418 -- Made generic views work with ModelForms

Generic views assumed any object's _meta will be model Options. This
is not true for ModelForms for example. Took isinstance(obj, Model)
in use instead.
  • Loading branch information...
commit 484fcd34a4819ff11ef72f3c9e1eb467a282b2f6 1 parent a035d9d
Anssi Kääriäinen authored June 09, 2012
7  django/views/generic/detail.py
... ...
@@ -1,6 +1,7 @@
1 1
 from __future__ import unicode_literals
2 2
 
3 3
 from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist
  4
+from django.db import models
4 5
 from django.http import Http404
5 6
 from django.utils.translation import ugettext as _
6 7
 from django.views.generic.base import TemplateResponseMixin, ContextMixin, View
@@ -81,7 +82,7 @@ def get_context_object_name(self, obj):
81 82
         """
82 83
         if self.context_object_name:
83 84
             return self.context_object_name
84  
-        elif hasattr(obj, '_meta'):
  85
+        elif isinstance(obj, models.Model):
85 86
             return obj._meta.object_name.lower()
86 87
         else:
87 88
             return None
@@ -128,13 +129,13 @@ def get_template_names(self):
128 129
 
129 130
         # The least-specific option is the default <app>/<model>_detail.html;
130 131
         # only use this if the object in question is a model.
131  
-        if hasattr(self.object, '_meta'):
  132
+        if isinstance(self.object, models.Model):
132 133
             names.append("%s/%s%s.html" % (
133 134
                 self.object._meta.app_label,
134 135
                 self.object._meta.object_name.lower(),
135 136
                 self.template_name_suffix
136 137
             ))
137  
-        elif hasattr(self, 'model') and hasattr(self.model, '_meta'):
  138
+        elif hasattr(self, 'model') and self.model is not None and issubclass(self.model, models.Model):
138 139
             names.append("%s/%s%s.html" % (
139 140
                 self.model._meta.app_label,
140 141
                 self.model._meta.object_name.lower(),
5  tests/regressiontests/generic_views/detail.py
@@ -92,3 +92,8 @@ def test_invalid_url(self):
92 92
 
93 93
     def test_invalid_queryset(self):
94 94
         self.assertRaises(ImproperlyConfigured, self.client.get, '/detail/author/invalid/qs/')
  95
+
  96
+    def test_non_model_object_with_meta(self):
  97
+        res = self.client.get('/detail/nonmodel/1/')
  98
+        self.assertEqual(res.status_code, 200)
  99
+        self.assertEqual(res.context['object'].id, "non_model_1")
2  tests/regressiontests/generic_views/urls.py
@@ -53,6 +53,8 @@
53 53
         views.AuthorDetail.as_view()),
54 54
     (r'^detail/author/invalid/qs/$',
55 55
         views.AuthorDetail.as_view(queryset=None)),
  56
+    (r'^detail/nonmodel/1/$',
  57
+        views.NonModelDetail.as_view()),
56 58
 
57 59
     # Create/UpdateView
58 60
     (r'^edit/artists/create/$',
15  tests/regressiontests/generic_views/views.py
@@ -226,3 +226,18 @@ class BookSigningTodayArchive(BookSigningConfig, generic.TodayArchiveView):
226 226
 
227 227
 class BookSigningDetail(BookSigningConfig, generic.DateDetailView):
228 228
     context_object_name = 'book'
  229
+
  230
+
  231
+class NonModel(object):
  232
+    id = "non_model_1"
  233
+
  234
+    _meta = None
  235
+
  236
+
  237
+class NonModelDetail(generic.DetailView):
  238
+
  239
+    template_name = 'generic_views/detail.html'
  240
+    model = NonModel
  241
+
  242
+    def get_object(self, queryset=None):
  243
+        return NonModel()

0 notes on commit 484fcd3

Please sign in to comment.
Something went wrong with that request. Please try again.