简体   繁体   中英

How to use new Django 1.2 readonly_fields in ModelForm

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 .

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.

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