Ticket #16101: patch_commit_9b22091813e7.diff

File patch_commit_9b22091813e7.diff, 5.7 KB (added by AndrewIngram, 13 years ago)

Added pk_url_kwarg and slug_url_kwarg to SingleObjectMixin so the defaults of 'pk' and 'slug' can be overridden.

  • django/views/generic/detail.py

    diff --git django/views/generic/detail.py django/views/generic/detail.py
    index 7e9047f..1802b33 100644
    class SingleObjectMixin(object):  
    1515    queryset = None
    1616    slug_field = 'slug'
    1717    context_object_name = None
     18    slug_url_kwarg = 'slug'
     19    pk_url_kwarg = 'pk'
    1820
    1921    def get_object(self, queryset=None):
    2022        """
    class SingleObjectMixin(object):  
    2931            queryset = self.get_queryset()
    3032
    3133        # Next, try looking up by primary key.
    32         pk = self.kwargs.get('pk', None)
    33         slug = self.kwargs.get('slug', None)
     34        pk = self.kwargs.get(self.get_pk_url_kwarg(), None)
     35        slug = self.kwargs.get(self.get_slug_url_kwarg(), None)
    3436        if pk is not None:
    3537            queryset = queryset.filter(pk=pk)
    3638
    class SingleObjectMixin(object):  
    7476        """
    7577        return self.slug_field
    7678
     79    def get_slug_url_kwarg(self):
     80        """
     81        Get the name of the URL keyword to be used to look up by slug.
     82        """
     83        return self.slug_url_kwarg
     84
     85    def get_pk_url_kwarg(self):
     86        """
     87        Get the name of the URL keyword to be used to look up by pk.
     88        """
     89        return self.pk_url_kwarg
     90
    7791    def get_context_object_name(self, obj):
    7892        """
    7993        Get the name to use for the object.
  • docs/ref/class-based-views.txt

    diff --git docs/ref/class-based-views.txt docs/ref/class-based-views.txt
    index e9f3152..8698161 100644
    SingleObjectMixin  
    136136        The name of the field on the model that contains the slug. By default,
    137137        ``slug_field`` is ``'slug'``.
    138138
     139    .. attribute:: slug_url_kwarg
     140
     141        The name of the URL keyword that contains the slug. By default,
     142        ``slug_url_kwarg`` is ``'slug'``.
     143
     144    .. attribute:: pk_url_kwarg
     145
     146        The name of the URL keyword that contains the primary key. By default,
     147        ``pk_url_kwarg`` is ``'pk'``.
     148
    139149    .. attribute:: context_object_name
    140150
    141151        Designates the name of the variable to use in the context.
    SingleObjectMixin  
    146156        ``queryset`` is provided, that queryset will be used as the
    147157        source of objects; otherwise,
    148158        :meth:`~SingleObjectMixin.get_queryset` will be used.
    149         :meth:`~SingleObjectMixin.get_object` looks for a ``pk``
    150         argument in the arguments to the view; if ``pk`` is found,
    151         this method performs a primary-key based lookup using that
    152         value. If no ``pk`` argument is found, it looks for a ``slug``
     159        :meth:`~SingleObjectMixin.get_object` looks for a
     160        :attr:`SingleObjectMixin.pk_url_kwarg` argument in the arguments
     161        to the view; if this argument is found, this method performs a
     162        primary-key based lookup using that value. If this argument is not
     163        found, it looks for a :attr:`SingleObjectMixin.slug_url_kwarg`
    153164        argument, and performs a slug lookup using the
    154165        :attr:`SingleObjectMixin.slug_field`.
    155166
  • tests/regressiontests/generic_views/detail.py

    diff --git tests/regressiontests/generic_views/detail.py tests/regressiontests/generic_views/detail.py
    index e8f2050..cd55d26 100644
    class DetailViewTest(TestCase):  
    2121        self.assertEqual(res.context['author'], Author.objects.get(pk=1))
    2222        self.assertTemplateUsed(res, 'generic_views/author_detail.html')
    2323
     24    def test_detail_by_custom_pk(self):
     25        res = self.client.get('/detail/author/bycustompk/1/')
     26        self.assertEqual(res.status_code, 200)
     27        self.assertEqual(res.context['object'], Author.objects.get(pk=1))
     28        self.assertEqual(res.context['author'], Author.objects.get(pk=1))
     29        self.assertTemplateUsed(res, 'generic_views/author_detail.html')
     30
    2431    def test_detail_by_slug(self):
    2532        res = self.client.get('/detail/author/byslug/scott-rosenberg/')
    2633        self.assertEqual(res.status_code, 200)
    2734        self.assertEqual(res.context['object'], Author.objects.get(slug='scott-rosenberg'))
    2835        self.assertEqual(res.context['author'], Author.objects.get(slug='scott-rosenberg'))
    2936        self.assertTemplateUsed(res, 'generic_views/author_detail.html')
     37   
     38    def test_detail_by_custom_slug(self):
     39        res = self.client.get('/detail/author/bycustomslug/scott-rosenberg/')
     40        self.assertEqual(res.status_code, 200)
     41        self.assertEqual(res.context['object'], Author.objects.get(slug='scott-rosenberg'))
     42        self.assertEqual(res.context['author'], Author.objects.get(slug='scott-rosenberg'))
     43        self.assertTemplateUsed(res, 'generic_views/author_detail.html')
    3044
    3145    def test_verbose_name(self):
    3246        res = self.client.get('/detail/artist/1/')
  • tests/regressiontests/generic_views/urls.py

    diff --git tests/regressiontests/generic_views/urls.py tests/regressiontests/generic_views/urls.py
    index 067c1f6..dc86e4d 100644
    urlpatterns = patterns('',  
    3030    url(r'^detail/author/(?P<pk>\d+)/$',
    3131        views.AuthorDetail.as_view(),
    3232        name="author_detail"),
     33    (r'^detail/author/bycustompk/(?P<foo>\d+)/$',
     34        views.AuthorDetail.as_view(pk_url_kwarg='foo')),                                             
    3335    (r'^detail/author/byslug/(?P<slug>[\w-]+)/$',
    3436        views.AuthorDetail.as_view()),
     37    (r'^detail/author/bycustomslug/(?P<foo>[\w-]+)/$',
     38        views.AuthorDetail.as_view(slug_url_kwarg='foo')),                       
    3539    (r'^detail/author/(?P<pk>\d+)/template_name_suffix/$',
    3640        views.AuthorDetail.as_view(template_name_suffix='_view')),
    3741    (r'^detail/author/(?P<pk>\d+)/template_name/$',
Back to Top