I'm trying to use the new readonly_fields in a ModelForm.
class TrainingAddForm(forms.ModelForm):
class Meta:
model = TrainingTasks
readonly_fields = ('trainee_signed','trainee_signed_date')
But this does not work. Am I missing something or is this not possible?
As per the documentation , this is a member of admin.ModelAdmin
, not forms.ModelForm
. Your admin form needs to inherit from admin.ModelAdmin
in order for you to have access to the readonly_fields
option.
Edit : I mis-read the original question, I thought you were trying to use the field within Django's supplied admin app. However, as seen in my initial response, this option is only available for classes that inherit from admin.ModelAdmin
— you will not be able to use it via forms.ModelForm
.
有关以表单的形式进行操作,请参见在Django表单中,如何将字段设置为只读(或禁用)以便无法对其进行编辑?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.