开发者

How to use new Django 1.2 readonly_fields in ModelForm

开发者 https://www.devze.com 2023-01-13 08:52 出处:网络
I\'m trying to use the new readonly_fields in a ModelForm. class TrainingAddForm(forms.ModelForm): class Meta:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消