我得到了以下模型:
class currency(models.Model):
currency = models.CharField(max_length=50)
dollaramount = models.DecimalField(max_digits=10, decimal_places=2)
def __unicode__(self):
return self.currency
该模型包含 2 个带有货币值“$”和“€”的条目
货币模型与以下表单中的 ModelChoiceField 相关,该表单具有用户定义的验证“clean_amount”,用于检查金额,具体取决于从 from 本身在货币字段中实际选择的货币。
如果所选对象的值为“$”或“€”,我如何在 userdef-validation“clean_amount”中进行比较?
from django import forms
from currencies.models import currency
class createform(forms.Form):
currency = forms.ModelChoiceField(queryset = currency.objects.all())
amount = forms.DecimalField(initial = 0)
def clean_amount(self):
currencyfield = self.cleaned_data['currency']
amount = self.cleaned_data['amount']
if amount < 0:
raise forms.ValidationError("Amount must be at least 0.")
#HERE I WANT TO CHECK
choosencurrency = currencyfield.??????
if choosencurrency == "$" :
#check $amount
if choosencurrency == "€" :
#check €amount
return amount