Ticket #15125: introspec_meta.diff

File introspec_meta.diff, 5.2 KB (added by John Anderson, 14 years ago)

patch with russelm's suggestions and some docs about the new method

  • docs/ref/class-based-views.txt

     
    140140
    141141        Designates the name of the variable to use in the context.
    142142
     143    .. method:: get_model()
     144        Retrieve the model to instantiate. By default
     145        :attr:`.model`.
     146
    143147    .. method:: get_object(queryset=None)
    144148
    145149        Returns the single object that this view will display. If
     
    488492        example, you could use ``success_url="/polls/%(slug)s/"`` to
    489493        redirect to a URL composed out of the ``slug`` field on a model.
    490494
     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
    491502    .. method:: get_form_class()
    492503
    493504        Retrieve the form class to instantiate. If
  • tests/regressiontests/generic_views/edit.py

     
    201201        except ImproperlyConfigured:
    202202            pass
    203203
     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
    204215class DeleteViewTests(TestCase):
    205216    urls = 'regressiontests.generic_views.urls'
    206217
  • tests/regressiontests/generic_views/urls.py

     
    11from django.conf.urls.defaults import *
    2 from django.views.generic import TemplateView
     2from django.views.generic import TemplateView, UpdateView
     3from regressiontests.generic_views.forms import AuthorForm
    34
    45import views
    56
     
    6465        views.NaiveAuthorUpdate.as_view()),
    6566    (r'^edit/author/(?P<pk>\d+)/update/redirect/$',
    6667        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/')),
    6771    (r'^edit/author/(?P<pk>\d+)/update/interpolate_redirect/$',
    6872        views.NaiveAuthorUpdate.as_view(success_url='/edit/author/%(id)d/update/')),
    6973    (r'^edit/author/(?P<pk>\d+)/update/$',
  • django/views/generic/detail.py

     
    1515    slug_field = 'slug'
    1616    context_object_name = None
    1717
     18    def get_model(self):
     19        return self.model
     20
    1821    def get_object(self, queryset=None):
    1922        """
    2023        Returns the object the view is displaying.
     
    5760        `get_object` is overridden.
    5861        """
    5962        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()
    6265            else:
    6366                raise ImproperlyConfigured(u"%(cls)s is missing a queryset. Define "
    6467                                           u"%(cls)s.model, %(cls)s.queryset, or override "
     
    127130                self.object._meta.object_name.lower(),
    128131                self.template_name_suffix
    129132            ))
    130         elif hasattr(self, 'model') and hasattr(self.model, '_meta'):
     133        elif hasattr(self, 'model') and hasattr(self.get_model(), '_meta'):
    131134            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(),
    134137                self.template_name_suffix
    135138            ))
    136139        return names
  • django/views/generic/edit.py

     
    6868    A mixin that provides a way to show and handle a modelform in a request.
    6969    """
    7070
     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
    7179    def get_form_class(self):
    7280        """
    7381        Returns the form class to use in this view
Back to Top