我有一个包含贷款金额的贷款模型。
我有一个包含一定数量的付款模型。
这是这两个模型的伪代码:
class Loan(models.Model):
loan_amount = PositiveIntegerField()
class Tranche(models.Model):
tranche_amount = PositiveIntegerField()
我为每个模型创建一个标准的ModelForm:
class LoanForm(forms.ModelForm):
class Meta:
model = Loan
fields=['loan_amount']
class TrancheForm(forms.ModelForm):
class Meta:
model = Tranche
fields = ['tranche_amount']
我定义了两种形式:
def someview(request):
loan_form = LoanForm(request.POST or None)
tranche_form = TrancheFrom(request.POST or None)
if loan_form.is_valid() and tranche_form.is_valid():
loan_form.save()
tranche_form.save()
#then render somme success_url
#else render the template and give it the forms
我希望了解一下清理后的贷款金额并将其提供给我的TrancheForm清理功能。
我要检查两个数量是否相同。
在我真正的例子中,Tranche是一个内联表单集,所以我想避免在视图级别上这样做:
if loan_form.loan_amount == tranche_form.loan_amount:
loan_form.save()
tranche_form.save()
else:
#return some error message
我希望有一种方法可以直接在clean方法中访问贷款金额。