3

In Django (we are currently using 1.9), when we add an UrlField to a model, the Admin site correctly renders the UrlField value as a clickable link on edit views.

If we were to mark this UrlField as readonly (through the ModelAdmin  readonly_fields attribute), the value is then displayed as non-clickable plain text.

  • What is a rationale for this behaviour ?
  • Is there a way to work around it without changing the widget for the associated form field ?
Ad N
  • 7,930
  • 6
  • 36
  • 80

1 Answers1

2

I think it's just that readonly_fields displays the raw content (using the __str__() method) without any widget.

To work it around you might do something like this:

class MyAdmin (ModelAdmin):
    readonly_fields = ['myurl_link']

    def myurl_link(self, instance):
        return format_html('<a href="{url}" target=_blank>{url}</a>', url=instance.myurl)
    myurl_link.short_description = _("Website")
Antoine Pinsard
  • 33,148
  • 8
  • 67
  • 87