Commit 1d0fc61b authored by Ramiro Morales's avatar Ramiro Morales Committed by Tim Graham
Browse files

Fixed #15185 -- Allowed ModelAdmin.list_display_links=None to disable change list links.

Thanks rm_ for the suggestion.
parent bf757a2f
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -794,7 +794,7 @@ class ModelAdmin(BaseModelAdmin):
        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:
        if self.list_display_links or self.list_display_links is None or not list_display:
            return self.list_display_links
        else:
            # Use only the first item in list_display as link
+9 −1
Original line number Diff line number Diff line
@@ -178,6 +178,14 @@ def items_for_result(cl, result, form):
    """
    Generates the actual list of data.
    """

    def link_in_col(is_first, field_name, cl):
        if cl.list_display_links is None:
            return False
        if is_first and not cl.list_display_links:
            return True
        return field_name in cl.list_display_links

    first = True
    pk = cl.lookup_opts.pk.attname
    for field_name in cl.list_display:
@@ -216,7 +224,7 @@ def items_for_result(cl, result, form):
            result_repr = mark_safe(' ')
        row_class = mark_safe(' class="%s"' % ' '.join(row_classes))
        # If list_display_links not defined, add the link tag to the first field
        if (first and not cl.list_display_links) or field_name in cl.list_display_links:
        if link_in_col(first, field_name, cl):
            table_tag = 'th' if first else 'td'
            first = False

+13 −10
Original line number Diff line number Diff line
@@ -257,8 +257,10 @@ class ModelAdminValidator(BaseValidator):
                                    % (cls.__name__, idx, field))

    def validate_list_display_links(self, cls, model):
        " Validate that list_display_links is a unique subset of list_display. "
        " Validate that list_display_links either is None or a unique subset of list_display."
        if hasattr(cls, 'list_display_links'):
            if cls.list_display_links is None:
                return
            check_isseq(cls, 'list_display_links', cls.list_display_links)
            for idx, field in enumerate(cls.list_display_links):
                if field not in cls.list_display:
@@ -344,6 +346,7 @@ class ModelAdminValidator(BaseValidator):
                    raise ImproperlyConfigured("'%s.list_editable[%d]' refers to "
                        "'%s' which is not defined in 'list_display'."
                        % (cls.__name__, idx, field_name))
                if cls.list_display_links is not None:
                    if field_name in cls.list_display_links:
                        raise ImproperlyConfigured("'%s' cannot be in both '%s.list_editable'"
                            " and '%s.list_display_links'"
+29 −13
Original line number Diff line number Diff line
@@ -648,19 +648,21 @@ subclass::

.. attribute:: ModelAdmin.list_display_links

    Set ``list_display_links`` to control which fields in ``list_display``
    should be linked to the "change" page for an object.
    Use ``list_display_links`` to control if and which fields in
    :attr:`list_display` should be linked to the "change" page for an object.

    By default, the change list page will link the first column -- the first
    field specified in ``list_display`` -- to the change page for each item.
    But ``list_display_links`` lets you change which columns are linked. Set
    ``list_display_links`` to a list or tuple of fields (in the same
    format as ``list_display``) to link.
    But ``list_display_links`` lets you change this:

    ``list_display_links`` can specify one or many fields. As long as the
    fields appear in ``list_display``, Django doesn't care how many (or
    how few) fields are linked. The only requirement is: If you want to use
    ``list_display_links``, you must define ``list_display``.
    * Set it to ``None`` to get no links at all.
    * Set it to a list or tuple of fields (in the same format as
      ``list_display``) whose columns you want converted to links.

      You can specify one or many fields. As long as the fields appear in
      ``list_display``, Django doesn't care how many (or how few) fields are
      linked. The only requirement is that if you want to use
      ``list_display_links`` in this fashion, you must define ``list_display``.

    In this example, the ``first_name`` and ``last_name`` fields will be
    linked on the change list page::
@@ -669,6 +671,16 @@ subclass::
            list_display = ('first_name', 'last_name', 'birthday')
            list_display_links = ('first_name', 'last_name')

    In this example, the change list page grid will have no links::

        class AuditEntryAdmin(admin.ModelAdmin):
            list_display = ('timestamp', 'message')
            list_display_links = None

    .. versionchanged:: 1.7

        ``None`` was added as a valid ``list_display_links`` value.

.. _admin-list-editable:

.. attribute:: ModelAdmin.list_editable
@@ -1242,9 +1254,13 @@ templates used by the :class:`ModelAdmin` views:

    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.
    It is expected to return either ``None`` or 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.

    .. versionchanged:: 1.7

        ``None`` was added as a valid ``get_list_display_links()`` return value.

.. method:: ModelAdmin.get_fields(self, request, obj=None)

+4 −0
Original line number Diff line number Diff line
@@ -164,6 +164,10 @@ Minor features
  new :func:`~django.contrib.admin.register` decorator to register a
  :class:`~django.contrib.admin.ModelAdmin`.

* You may specify :meth:`ModelAdmin.list_display_links
  <django.contrib.admin.ModelAdmin.list_display_links>` ``= None`` to disable
  links on the change list page grid.

:mod:`django.contrib.auth`
^^^^^^^^^^^^^^^^^^^^^^^^^^

Loading