简体   繁体   English

如何在ModelForm中使用新的Django 1.2 readonly_fields

[英]How to use new Django 1.2 readonly_fields in ModelForm

I'm trying to use the new readonly_fields in a ModelForm. 我正在尝试在ModelForm中使用新的readonly_fields。

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 . 根据文档 ,这是admin.ModelAdmin的成员,而不是forms.ModelForm的成员。 Your admin form needs to inherit from admin.ModelAdmin in order for you to have access to the readonly_fields option. 您的管理表单需要从admin.ModelAdmin继承,以便您可以访问readonly_fields选项。

Edit : I mis-read the original question, I thought you were trying to use the field within Django's supplied admin app. 编辑 :我误读了原始问题,我以为您正在尝试使用Django提供的管理应用程序中的字段。 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 . 但是,从我的初始响应中可以看出,此选项仅适用于从admin.ModelAdmin继承的类-您将无法通过forms.ModelForm使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM