This is an improvement using this answer, allowing you to supply an optional alternative short description instead of the supplied field_name
being used for the column header. And also an optional label_prop
value to display an alternative property value of the model if so desired
EDIT: I've also now updated it to handle ManyToManyFields
from typing import Optional
from django.contrib import admin
from django.contrib.contenttypes.models import ContentType
from django.db.models import ManyToManyField, ForeignKey, Model
from django.urls import reverse
from django.utils.html import format_html
def create_link(linked_obj, app_label: str, label_prop: Optional[str] = None) -> str:
linked_content_type = ContentType.objects.get_for_model(linked_obj)
model_name = linked_content_type.model
view_name = f"admin:{app_label}_{model_name}_change"
link_url = reverse(view_name, args=[linked_obj.pk])
return "<a href='%s'>%s</a>" % (link_url, getattr(linked_obj, label_prop) if label_prop else linked_obj)
def linkify(field_name: str, label_prop: Optional[str] = None, short_description: Optional[str] = None, as_html_list: bool = False):
def _linkify(obj: Model):
content_type = ContentType.objects.get_for_model(obj)
app_label = content_type.app_label
field_type = obj._meta.get_field(field_name)
items = None
if isinstance(field_type, ManyToManyField):
items = list(getattr(obj, field_name).all())
elif isinstance(field_type, ForeignKey):
items = [getattr(obj, field_name)]
else:
print(f'field_name {field_name} is not ManyToManyField or ForeignKey')
links = [create_link(itm, app_label, label_prop) for itm in items if itm is not None]
if len(links) > 1:
if as_html_list:
html = "<ul>"
for link in links:
html += f'<li>{link}</li>'
html += "</ul>"
else:
html = ", ".join(links)
else:
html = links[0]
return format_html(html)
_linkify.short_description = [short_description, field_name.replace("_", " ").capitalize()][short_description is None]
return _linkify
Example usage:
Models
# models.py
from django.db import models
class Country(models.Model):
name = models.CharField(max_length=200)
population = models.IntegerField()
class Career(models.Model):
name = models.CharField(max_length=200)
average_salary = models.IntegerField()
class Person(models.Model):
name = models.CharField(max_length=200)
age = models.IntegerField()
country = models.ForeignKey(Country, on_delete=models.CASCADE)
career = models.ForeignKey(Career, on_delete=models.CASCADE)
Admin
@admin.register(Person)
class PersonAdmin(admin.ModelAdmin):
list_display = [
"name",
"age",
linkify(
field_name="country",
label_prop="name",
short_description="Country Name"
),
linkify(field_name="career"),
]
Result
Given the name
of Adam's country is France.
| Name | Age | Country Name |...|
|------|-----|----------------------------------------------------------|...|
| Adam | 20 | <a href="/admin/app/country/123">France</a> |...|
It's also worth noting...
...the linkify
function can also be used along side the display decorator
@admin.register(Person)
class PersonAdmin(admin.ModelAdmin):
list_display = [
"name",
"age",
"sortable_country_link",
linkify(field_name="career"),
]
@admin.display(description='Country Name', ordering='country__name')
def sortable_country_link(self, obj):
return linkify(field_name='country', label_prop='name')(obj)
linked_obj.id
bylinked_obj.pk
to deal with custom pks. – Oaxaca