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?
For doing it in a form, see In a Django form, how do I make a field readonly (or disabled) so that it cannot be edited?
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
.
精彩评论