Django — How to include links in ModelAdmin Inlines


Just like with ModelAdmins, we can include arbitrary python code in our inlines.

For a ModelAdmin, I often set up a python function that returns color coded html or processes data from the object somehow.

With Inlines, you might get an error that the field doesn’t exist in the form — the trick is to add the method you are adding as a readonly_field.

class OrderInline(admin.TabularInline):
    model = Order
    fields = ('total', 'tasting_date', '_link')
    readonly_fields = ('_link', )

    def _link(self, obj):
        return 'hello'


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s