django admin: change_form.html で読み取り専用 URL フィールドをクリック可能にするにはどうすればいいですか? 質問する

django admin: change_form.html で読み取り専用 URL フィールドをクリック可能にするにはどうすればいいですか? 質問する

change_form ページの admin で読み取り専用 URL フィールドをクリック可能にしたいと考えています。ウィジェットを試してみましたが、ウィジェットはフォーム フィールド専用であることがすぐにわかりました。そのため、jQuery (検索と置換など) を使用してこの問題を解決しようとする前に、Python でこれに対するよりエレガントなソリューションがあるかどうかを知りたいです。何かアイデアはありますか?

ベストアンサー1

古い質問ですが、まだ答える価値があります。

ドキュメントを参照readonly_fields現在ではそれらのカスタマイズ方法もサポートしており、リンクコメントに投稿:

def the_callable(obj):
    return u'<a href="#">link from the callable for {0}</a>'.format(obj)
the_callable.allow_tags = True

class SomeAdmin(admin.ModelAdmin):
    def the_method_in_modeladmin(self, obj):
         return u'<a href="#">link from the method of modeladmin for {0}</a>'.format(obj)
    the_method_in_modeladmin.allow_tags = True

    readonly_fields = (the_callable, 'the_method_in_modeladmin', 'the_callable_on_object')

ObjModel.the_callable_on_object = lambda self, obj: u'<a href="#">link from the callable of the instance </a>'.format(obj)
ObjModel.the_callable_on_object.__func__.allow_tags = True

上記のコードは、変更フォーム ページに 3 つの読み取り専用フィールドをレンダリングします。

おすすめ記事