Django UpdateView disable some fields

后端 未结 2 2005
抹茶落季
抹茶落季 2021-01-03 13:14

I have made a class view inheriting UpdateView. I have specified the fields and models from which the forms should be built. Now say if i have a field email, then I want to

2条回答
  •  囚心锁ツ
    2021-01-03 13:41

    Define a UserForm with exclude fields which you don't want to show in the form

    class UserForm(forms.ModelForm):
        class Meta:
            model = Users
            exclude = ('email',) # add fields to disable it in the form
    

    If you want to make field readonly in > django 1.9 use disabled

    class UserForm(forms.ModelForm):
        email =  forms.CharField(disabled=True)
        class Meta:
            model = Users
            fields = ['email', 'first_name', 'last_name', 'birth_date']
    

    Then specify form in view.

    class UserUpdate(UpdateView):
        model = Users
        form_class = UserForm
        ....
    

提交回复
热议问题