gpt4 book ai didi

python - Django 模型形式 : Exclude a field which is not in Model

转载 作者:太空宇宙 更新时间:2023-11-04 02:41:51 24 4
gpt4 key购买 nike

我很难调试它。我想在扩展的 modelform UpdateRegModelForm 中排除 RegModelFormconfirm_password。我试图在 UpdateRegModelForm 的 Meta 类中使用 exclude 但它似乎在呈现 UpdateRegModelForm 时显示 confirm_password .不确定如何前进。

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',)

最佳答案

fieldsexclude 属性仅与从模型创建的字段相关。由于您已直接在表单本身中指定了 confirm_password,因此它将始终存在。

删除的方法是从表单的fields字典中删除。您可以在 __init__ 方法中执行此操作:

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

你根本不需要在这个子类中定义 Meta。

关于python - Django 模型形式 : Exclude a field which is not in Model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46261857/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com