2017-09-17 2 views
2

これをデバッグするのに苦労しています。私は、modelformUpdateRegModelFormののRegModelFormを拡張して除外したいと考えています。私はUpdateRegModelFormのメタクラスでexcludeを使用しようとしましたが、とにかくをレンダリングしている間にconfirm_passwordと表示されているようです。どのように前進するかわからない。Django ModelForm:モデルにないフィールドを除外します

class RegModelForm(forms.ModelForm): 

    org_admin_email = forms.CharField(
     label='If you know who should be the Admin, please add their email address below.' 
       ' We\'ll send them an email inviting them to join the platform as the organization admin.', 
     required=False, 
     widget=forms.EmailInput(attrs=({'placeholder': 'Email'})) 
    ) 
    organization_name = forms.CharField(
     max_length=255, 
     label='Organization Name', 
     widget=forms.TextInput(
      attrs={'placeholder': 'Organization Name'} 
     ) 
    ) 

    confirm_password = forms.CharField(
     label='Confirm Password', widget=forms.PasswordInput(attrs={'placeholder': 'Confirm Password'}) 
    ) 

    class Meta: 
     model = ExtendedProfile 

     fields = (
      'confirm_password', 'first_name', 'last_name', 
      'organization_name', 'is_currently_employed', 'is_poc', 'org_admin_email', 
     ) 

     labels = { 
      'is_currently_employed': "Check here if you're currently not employed.", 
      'is_poc': 'Are you the Admin of your organization?' 
     } 

     widgets = { 
      'first_name': forms.TextInput(attrs={'placeholder': 'First Name'}), 
      'last_name': forms.TextInput(attrs={'placeholder': 'Last Name'}), 
      'is_poc': forms.RadioSelect() 
     } 


class UpdateRegModelForm(RegModelForm): 
    class Meta(RegModelForm.Meta): 
     exclude = ('confirm_password',) 

答えて

2

fieldsexclude属性は唯一のモデルから作成されたフィールドに関連しています。フォーム自体にconfirm_passwordを直接指定したので、それは常に存在します。

削除する方法は、フォームのfields辞書から削除することです。あなたは__init__方法でこれを行うことができます。

class UpdateRegModelForm(RegModelForm): 
    def __init__(self, *args, **kwargs): 
     super(UpdateRegModelForm, self).__init__(*args, **kwargs) 
     self.fields.pop('confirm_password') 

あなたは全くこのサブクラスでメタを定義する必要はありません。

関連する問題