Login

Additional Change List Columns

Author:
sansmojo
Posted:
July 23, 2007
Language:
Python
Version:
.96
Tags:
template admin tags change_list
Score:
1 (after 3 ratings)

Ever wanted to add a link or two at the end of a row in a model's change list? Say you had a model for people and a model for registrations or blog posts and you want to modify the people change list so it has a link to, say all of the registrations or blog posts for the person.

Well, Django provides half of the solution already in that the example registration change_list already handles the display of all registrations tied to that person. For example, the url /admin/registrations/registration/?person__id__exact=121 gets you to a filtered list of registrations for the person with the id of 121. This is the same url used if you use list_filter in your model definition (though setting list_filter is not required for what we're doing).

Okay, so to add a link to the end of each person row in the change list, you need to create a template tag similar to "person_result_list" in the code. There, I've given an example that adds two links. Each dictionary in additional_links needs to have at least a text, sortable, and url_template attribute. The text attribute is what will display as the header to the column. url_template will be fed the id of the row object (in this example, a person id), which you can use to construct the link for each row. You could extend this if you wish, though all I ever need is the id.

And the last step is to use your new template tag in a modified change_list.html in place of the default result_list tag. The example at the bottom of the code shows an example usage of the tag.

Hope this makes sense and is helpful to someone!

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from django.contrib.admin.templatetags.admin_list import items_for_result, result_headers
from django.template import Library
from django.utils.safestring import mark_safe
register = Library()

def results(cl, additional_links):
    """
    Rewrite of original function to add additional columns after each result
    in the change list.
    """
    for res in cl.result_list:
        rl = list(items_for_result(cl,res))
        for link in additional_links:
            rl.append(mark_safe(link['url_template'] % res.id))
        yield rl

def extended_result_list(cl, additional_links):
    """
    Rewrite of original function to add an additional columns after each result
    in the change list.
    """
    headers = list(result_headers(cl))
    for header in additional_links:
        headers.append(header)

    return {
        'cl': cl,
        'result_headers': headers,
        'results': list(results(cl, additional_links))
    }

# This function is an example template tag for use in an overridden change_list.html template.
def person_result_list(cl):
    additional_links = (
        { 'text': 'All Registrations',
          'sortable': False,
          'url_template': '<td><a href="/ce/admin/registrations/registration/?person__id__exact=%s">All Registrations</a></td>'
        },
        { 'text': 'New Registration',
          'sortable': False,
          'url_template': '<td><a href="/ce/admin/registrations/registration/add/?person_id=%s">New Registration</a></td>',
        },
    )

    return extended_result_list(cl, additional_links)
person_result_list = register.inclusion_tag("admin/change_list_results.html")(person_result_list)

# Modified section of change_list.html template.
{# Override the original result_list templatetag to add a registration link for each person #}
{# {% block result_list %}{% result_list cl %}{% endblock %} #}
{% load admin_extras %}
{% block result_list %}{% person_result_list cl %}{% endblock %}

More like this

  1. FilterSpec for ForeignKeys to auth.User with HTML input tag for ModelAdmin.list_filter by loic 4 years, 3 months ago
  2. Compact list_filter by onlinehero 5 years, 7 months ago
  3. Filter change list by a date range by aruseni 3 years, 6 months ago
  4. Custom change_list filter based on SimpleListFilter shows only referenced (related, used) values by darklow 2 years, 7 months ago
  5. a template tag to invoke a method on an object with a variable by Scanner 8 years, 3 months ago

Comments

mistercrunch (on September 23, 2010):

I was trying to find a way to do this, and found a much easier way: Add a function to the model that returns a html [HTML_REMOVED][HTML_REMOVED] tag Set the function's "allow_tags" property to True so that html is rendered correctly (not as text) * You can even set the function's "short_description" attribute for the table header

Example (sorry indentation doesn't work in comments...)

class Project(models.Model):

#Your model definitoin here

def link_to(self):

  return("<a href='../somewhere/" + self.id + "/'>View Detail</a>")

link_to.allow_tags = True

link_to.short_description = 'Link'

#

jultra (on June 23, 2012):

Hi,

I know this is an old post. But I think I need this snippet now. I'm just not sure where to put it. So where should I put this code? Inside the model definition?

Thank you!

#

Please login first before commenting.