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 %}
Comments
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):
link_to.allow_tags = True
link_to.short_description = 'Link'
#
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!
#