d33tah
d33tah

Reputation: 11561

Display a custom link in Django Admin edit item view?

Here's a piece of Django admin interface's instance edition form:

enter image description here

How should I change the underlying admin.ModelAdmin instance to make it contain an URL, like this?

enter image description here

Upvotes: 2

Views: 1928

Answers (1)

JCotton
JCotton

Reputation: 11760

Django makes this easy. Subclass ModelAdmin, add a custom method and then tell the Admin how to use it. Here's a sample admin.py:

from django.contrib import admin
from .models import Vendor

class VendorAdmin(admin.ModelAdmin):
    readonly_fields = ['example_link']

    def example_link(self, obj):
        return '<a href="{}">link text</a>'.format(obj.get_link())  # however you generate the link
    example_link.allow_tags = True

admin.site.register(Vendor, VendorAdmin)

Here's the documentation that furthers explains readonly_fields, customizing the form label text with short_description, ordering, and how you can put this custom url method on the Model or ModelAdmin.

Upvotes: 7

Related Questions