Ticket #15125: introspec_meta.diff
File introspec_meta.diff, 5.2 KB (added by , 14 years ago) |
---|
-
docs/ref/class-based-views.txt
140 140 141 141 Designates the name of the variable to use in the context. 142 142 143 .. method:: get_model() 144 Retrieve the model to instantiate. By default 145 :attr:`.model`. 146 143 147 .. method:: get_object(queryset=None) 144 148 145 149 Returns the single object that this view will display. If … … 488 492 example, you could use ``success_url="/polls/%(slug)s/"`` to 489 493 redirect to a URL composed out of the ``slug`` field on a model. 490 494 495 .. method:: get_model() 496 Retrieve the model to instantiate. By default 497 :attr:`~SingleObjectMixin.model` will be used if provided. 498 Otherwise it'll use :attr:`~FormMixin.form_class` and lastly 499 :attr:`~SingleObjectMixin.queryset`, which will raise an 500 ImproperlyConfigured exception if none of them are set. 501 491 502 .. method:: get_form_class() 492 503 493 504 Retrieve the form class to instantiate. If -
tests/regressiontests/generic_views/edit.py
201 201 except ImproperlyConfigured: 202 202 pass 203 203 204 def test_update_with_form_class(self): 205 a = Author.objects.create( 206 name='Randall Munroe', 207 slug='randall-munroe', 208 ) 209 res = self.client.post('/edit/author/%d/update/formclass/' % a.pk, 210 {'name': 'Randall Munroe (author of xkcd)', 'slug': 'randall-munroe'}) 211 self.assertEqual(res.status_code, 302) 212 self.assertRedirects(res, 'http://testserver/edit/authors/create/') 213 self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe (author of xkcd)>']) 214 204 215 class DeleteViewTests(TestCase): 205 216 urls = 'regressiontests.generic_views.urls' 206 217 -
tests/regressiontests/generic_views/urls.py
1 1 from django.conf.urls.defaults import * 2 from django.views.generic import TemplateView 2 from django.views.generic import TemplateView, UpdateView 3 from regressiontests.generic_views.forms import AuthorForm 3 4 4 5 import views 5 6 … … 64 65 views.NaiveAuthorUpdate.as_view()), 65 66 (r'^edit/author/(?P<pk>\d+)/update/redirect/$', 66 67 views.NaiveAuthorUpdate.as_view(success_url='/edit/authors/create/')), 68 (r'^edit/author/(?P<pk>\d+)/update/formclass/$', 69 UpdateView.as_view(form_class=AuthorForm, 70 success_url='/edit/authors/create/')), 67 71 (r'^edit/author/(?P<pk>\d+)/update/interpolate_redirect/$', 68 72 views.NaiveAuthorUpdate.as_view(success_url='/edit/author/%(id)d/update/')), 69 73 (r'^edit/author/(?P<pk>\d+)/update/$', -
django/views/generic/detail.py
15 15 slug_field = 'slug' 16 16 context_object_name = None 17 17 18 def get_model(self): 19 return self.model 20 18 21 def get_object(self, queryset=None): 19 22 """ 20 23 Returns the object the view is displaying. … … 57 60 `get_object` is overridden. 58 61 """ 59 62 if self.queryset is None: 60 if self. model:61 return self. model._default_manager.all()63 if self.get_model(): 64 return self.get_model()._default_manager.all() 62 65 else: 63 66 raise ImproperlyConfigured(u"%(cls)s is missing a queryset. Define " 64 67 u"%(cls)s.model, %(cls)s.queryset, or override " … … 127 130 self.object._meta.object_name.lower(), 128 131 self.template_name_suffix 129 132 )) 130 elif hasattr(self, 'model') and hasattr(self. model, '_meta'):133 elif hasattr(self, 'model') and hasattr(self.get_model(), '_meta'): 131 134 names.append("%s/%s%s.html" % ( 132 self. model._meta.app_label,133 self. model._meta.object_name.lower(),135 self.get_model()._meta.app_label, 136 self.get_model()._meta.object_name.lower(), 134 137 self.template_name_suffix 135 138 )) 136 139 return names -
django/views/generic/edit.py
68 68 A mixin that provides a way to show and handle a modelform in a request. 69 69 """ 70 70 71 def get_model(self): 72 model = super(ModelFormMixin, self).get_model() 73 74 if model: 75 return model 76 elif self.get_form_class(): 77 return self.get_form_class()._meta.model 78 71 79 def get_form_class(self): 72 80 """ 73 81 Returns the form class to use in this view