【发布时间】:2017-08-01 12:21:14
【问题描述】:
在我的forms.py 文件中,我有一个给我带来大问题的类:
class CustomerBaseForm(forms.Form):
...
def clean(self):
cleaned_data = super(CustomerBaseForm, self).clean()
bank_account = cleaned_data.get("bank_account")
ssn = self.cleaned_data.get('ssn')
bank = self.cleaned_data.get('bank')
bank_transit = self.cleaned_data.get('bank_transit')
v = CustomerProfileFormCleaner(self)
v.clean()
# The concatenation of bank transit, the bank account and the bank
# number must be unique. Hence, the following message would be
# displayed if it is already in use.
msg = _('The bank, the bank transit and the bank account are already in use')
customer = CustomerProfile.objects.filter(ssn=ssn)
qs = FinancialProfile.objects.filter(
bank=bank,
bank_transit=bank_transit,
bank_account=bank_account)
if customer.count() == 1:
qs = qs.exclude(customer_id__in=[cust.id for cust in customer])
if qs.count() > 0:
self.add_error('bank_account', msg)
self.add_error('bank', '')
self.add_error('bank_transit', '')
即使我有ValidationError 和forms.Form,我如何保存bank、bank_transit 和bank_account 字段?如果ValidationError 阻止我保存字段,我认为可以强制保存它们。如果没有,是否有解决方法可以做到这一点?
我以为我可以在课堂上做这样的事情,但不清楚..
class Meta:
model = FinancialProfile
exclude = ['bank', 'bank_transit', 'bank_account']
【问题讨论】:
标签: django forms unit-testing