- Create a new method in your ModelAdmin class that returns the link for the column.
- Use the admin list_display attribute to specify the new method for the desired column.
- Use the format_html method from django.utils.html to generate the HTML link tag.
- Enclose the link text and URL in the format_html method.
from django.contrib import admin
from django.utils.html import format_html
from .models import YourModel
class YourModelAdmin(admin.ModelAdmin):
def link_to_example(self, obj):
url = obj.get_absolute_url() # Replace with the actual method to get the URL
return format_html('<a href="{}">Link Text</a>', url)
link_to_example.short_description = 'Example' # Replace with desired column header text
list_display = ('id', 'other_fields', 'link_to_example') # Add 'link_to_example' to the list