Commit 9796f695 authored by Julien Phalip's avatar Julien Phalip
Browse files

Fixed #16257 -- Added new `ModelAdmin.get_list_display_links()` method to...

Fixed #16257 -- Added new `ModelAdmin.get_list_display_links()` method to allow for the dynamic display of links on the admin changelist. Thanks to graveyboat for the suggestion and initial patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17037 bcc190cf-cafb-0310-a4f2-bffc1f526a37
parent a05c70fa
Loading
Loading
Loading
Loading
+18 −8
Original line number Diff line number Diff line
@@ -650,6 +650,18 @@ class ModelAdmin(BaseModelAdmin):
        """
        return self.list_display

    def get_list_display_links(self, request, list_display):
        """
        Return a sequence containing the fields to be displayed as links
        on the changelist. The list_display parameter is the list of fields
        returned by get_list_display().
        """
        if self.list_display_links or not list_display:
            return self.list_display_links
        else:
            # Use only the first item in list_display as link
            return list(list_display)[:1]

    def construct_change_message(self, request, form, formsets):
        """
        Construct a change message from a changed object.
@@ -1087,22 +1099,20 @@ class ModelAdmin(BaseModelAdmin):

    @csrf_protect_m
    def changelist_view(self, request, extra_context=None):
        "The 'change list' admin view for this model."
        """
        The 'change list' admin view for this model.
        """
        from django.contrib.admin.views.main import ERROR_FLAG
        opts = self.model._meta
        app_label = opts.app_label
        if not self.has_change_permission(request, None):
            raise PermissionDenied

        # Check actions to see if any are available on this changelist
        actions = self.get_actions(request)

        list_display = self.get_list_display(request)
        list_display_links = self.get_list_display_links(request, list_display)

        list_display_links = self.list_display_links
        if not self.list_display_links and list_display:
            list_display_links = list(list_display)[:1]

        # Check actions to see if any are available on this changelist
        actions = self.get_actions(request)
        if actions:
            # Add the action checkboxes if there are any actions available.
            list_display = ['action_checkbox'] +  list(list_display)
+10 −0
Original line number Diff line number Diff line
@@ -1044,6 +1044,16 @@ templates used by the :class:`ModelAdmin` views:
    displayed on the changelist view as described above in the
    :attr:`ModelAdmin.list_display` section.

.. method:: ModelAdmin.get_list_display_links(self, request, list_display)

    .. versionadded:: 1.4

    The ``get_list_display_links`` method is given the ``HttpRequest`` and
    the ``list`` or ``tuple`` returned by :meth:`ModelAdmin.get_list_display`.
    It is expected to return a ``list`` or ``tuple`` of field names on the
    changelist that will be linked to the change view, as described in the
    :attr:`ModelAdmin.list_display_links` section.

.. method:: ModelAdmin.get_urls(self)

    The ``get_urls`` method on a ``ModelAdmin`` returns the URLs to be used for
+10 −3
Original line number Diff line number Diff line
@@ -124,13 +124,20 @@ to work similarly to how desktop GUIs do it. The new hook
:meth:`~django.contrib.admin.ModelAdmin.get_ordering` for specifying the
ordering dynamically (e.g. depending on the request) has also been added.

``ModelAdmin.save_related()``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
New ``ModelAdmin`` methods
~~~~~~~~~~~~~~~~~~~~~~~~~~

A new :meth:`~django.contrib.admin.ModelAdmin.save_related` hook was added to
A new :meth:`~django.contrib.admin.ModelAdmin.save_related` method was added to
:mod:`~django.contrib.admin.ModelAdmin` to ease the customization of how
related objects are saved in the admin.

Two other new methods,
:meth:`~django.contrib.admin.ModelAdmin.get_list_display` and
:meth:`~django.contrib.admin.ModelAdmin.get_list_display_links`
were added to :class:`~django.contrib.admin.ModelAdmin` to enable the dynamic
customization of fields and links to display on the admin
change list.

Admin inlines respect user permissions
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+10 −3
Original line number Diff line number Diff line
@@ -58,13 +58,20 @@ class ChordsBandAdmin(admin.ModelAdmin):


class DynamicListDisplayChildAdmin(admin.ModelAdmin):
    list_display = ('name', 'parent')
    list_display = ('parent', 'name', 'age')

    def get_list_display(self, request):
        my_list_display = list(self.list_display)
        my_list_display = super(DynamicListDisplayChildAdmin, self).get_list_display(request)
        if request.user.username == 'noparents':
            my_list_display = list(my_list_display)
            my_list_display.remove('parent')

        return my_list_display

class DynamicListDisplayLinksChildAdmin(admin.ModelAdmin):
    list_display = ('parent', 'name', 'age')
    list_display_links = ['parent', 'name']

    def get_list_display_links(self, request, list_display):
        return ['age']

site.register(Child, DynamicListDisplayChildAdmin)
+1 −0
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ class Parent(models.Model):
class Child(models.Model):
    parent = models.ForeignKey(Parent, editable=False, null=True)
    name = models.CharField(max_length=30, blank=True)
    age = models.IntegerField(null=True, blank=True)

class Genre(models.Model):
    name = models.CharField(max_length=20)
Loading