Skip to main content

django-admin-fk-links

Build status PyPI version PyPI - Python Version PyPI - Django Version Downloads

Reusable Django admin mixin that turns ForeignKey fields into direct clickable links to their related admin change views.


✨ Features

  • ✅ Converts ForeignKey fields into clickable links in list_display
  • ✅ Works with the default Django admin and custom AdminSite
  • ✅ Zero configuration
  • ✅ No need to add to INSTALLED_APPS
  • ✅ Fully compatible with Django 2.2+

📦 Installation

pip install django-admin-fk-links

🚀 Quick Usage

from django.contrib import admin
from django_admin_fk_links import ForeignKeyLinkMixin

@admin.register(Book)
class BookAdmin(ForeignKeyLinkMixin, admin.ModelAdmin):
    list_display = ("title", "author")
    list_display_foreign_key_links = ("author",)

That’s it. The author column will now be a direct link to its admin change view.


🖼️ Screenshots

Before — author rendered as plain text:

Book changelist without the mixin

After — author rendered as a clickable link to its change view:

Book changelist with the mixin


⚙️ How It Works

The mixin dynamically replaces the fields listed in:

list_display_foreign_key_links = ("field_name",)

with callables that render an <a> tag pointing to the related object’s admin change view.

It also supports:

  • Sorting via admin_order_field
  • Automatic verbose_name resolution
  • Custom AdminSite namespaces

Override get_foreign_key_link() to change the markup — return safe HTML (format_html()), since the admin escapes plain strings:

from django.utils.html import format_html


class BookAdmin(ForeignKeyLinkMixin, admin.ModelAdmin):
    list_display = ("title", "author")
    list_display_foreign_key_links = ("author",)

    def get_foreign_key_link(self, obj, field_name, related, url):
        return format_html('<a class="button" target="_blank" href="{}">{}</a>', url, related)

Or render it from a template, with obj, related, url, field_name and link_class in its context:

class BookAdmin(ForeignKeyLinkMixin, admin.ModelAdmin):
    list_display = ("title", "author")
    list_display_foreign_key_links = ("author",)
    foreign_key_link_template = "admin/book_author_link.html"
{# templates/admin/book_author_link.html #}
<a class="fk-link" href="{{ url }}" title="{{ obj }}">{{ related }}</a>

To only add a CSS class, set foreign_key_link_class = "my-link".


🎨 Themes


✅ Compatibility

  • Django 2.2+
  • Python 3.7+
  • Default admin.site ✅
  • Custom AdminSite(name="custom") ✅

🪪 License

This project is licensed under the MIT License.


🤝 Contributing

Contributions, issues and feature requests are welcome. Feel free to open a PR or issue.

⭐ If you find it useful

Please consider giving the project a ⭐ on GitHub — it really helps!

Metadata

Release files for django-admin-fk-links 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-admin-fk-links 0.1.4
File Size Uploaded
django_admin_fk_links-0.1.4.tar.gz 129.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-admin-fk-links 0.1.4
File Interpreter ABI Platform
django_admin_fk_links-0.1.4-py3-none-any.whl Python 3 none any Details

Total release size: 135.5 kB

Release history Release notifications | RSS feed

This release

0.1.4 This release

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page