本文介绍了django admin如何在readonly字段上显示窗口小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

admin.py



admin.py

class AudioTrackAdminInline(admin.StackedInline):
    model = AudioTrack
    form = AudioTrackForm
    readonly_fields = ('file',)

forms.py

class AudioTrackForm(forms.ModelForm):
    class Meta:
        model = AudioTrack
        widgets = { 'file': MediaFileInput, } # my widget

当文件不是只读时,它会显示窗口小部件确定。但是当我把它包含成readonly时,我会看到文本行。 (Django不要使用我的表单,如果readonly)

When the file is not readonly, it displays widget OK. But when i include it as readonly, i see text line. (Django does not to use my form if readonly)

我如何得到它使用形式即使在只读字段?

How can i get it to use form even at readonly field?

如果我无法设置我的字段,如何显示另一个小部件?

How to display another widget if i set my field readonly?

推荐答案

我会说这是一个功能。

当字段设置为只读时,Django使用功能,可以硬编码结果,您无法自定义。

When field is set to readonly, Django uses display_for_field function which hardcodes the result, you can't customize it.

另一种方法是不要将该字段设置为只读,并覆盖 ,一个未记录的 ModelAdmin 方法,并根据您的需要进行操作。

Another approach would be not to set the field as readonly and override formfield_for_dbfield, an undocumented ModelAdmin method and act accordingly to your needs.

这篇关于django admin如何在readonly字段上显示窗口小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 20:05