Additional Change List Columns

 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 2 years ago
  2. Add special field lookups to the Admin list_filter display by whiteinge 5 years, 3 months ago
  3. Dynamically insert or append a value to an admin option, e.g. list_display or list_filter by frankban 1 year, 9 months ago
  4. Yet another list partitioning filter by AndrewIngram 4 years ago
  5. Filter change list by a date range by aruseni 1 year, 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!

#

(Forgotten your password?)