わかりましたので、ドロップダウンの値に応じて特定のフィールドのみを入力する必要があるフォームがありますが、「discountpercentage」と呼ばれるキーのキーエラーが発生します。空白のままにすると、 clean_data を実行してから、カスタム クリーンを実行しようとすると、見つからないためキー エラーが発生します。
def clean(self):
data = self.cleaned_data
print(str(data))
#try:
# if data['discountcode']:
# code = data['discountcode']
# try:
# DiscountCode.objects.filter(discountcode=code)
# self._errors["discountcode"] = ErrorList([u"Discount code must be unique."])
# except:
# pass
#except:
# pass
if data['discounton'] == '1' and not data['discountitem']:
self._errors["discounton"] = ErrorList([u"Please select items to apply discount on."])
elif data['discounton'] == '2' and not data['discountcategory']:
self._errors["discounton"] = ErrorList([u"Please select category to apply discount on."])
elif data['discounton'] == '3':
pass
if data['discounttype'] == '1' and not data['discountpercentage']:
self._errors["discounttype"] = ErrorList([u"Please provide a percentage to discount."])
elif data['discounttype'] == '2' and not data['discountvalue']:
self._errors["discounttype"] = ErrorList([u"Please provide a value to discount."])
if data['discountexpiry'] == '1' and not data['discountexpiryuse']:
self._errors["discountexpiry"] = ErrorList([u"Please provide a redeem limit."])
elif data['discountexpiry'] == '2' and not data['discountexpirydate']:
self._errors["discountexpiry"] = ErrorList([u"Please provide a date disable this discount code."])
elif data['discountexpiry'] == '3':
pass
return data
そして、discounttype == '1' を指定し、discountpercentage を空白にして、cleaned_data を出力すると、次のようになります。
{'uselimit': u'3', 'discounton': u'1', 'discountcode': u'uyhgkjhg', 'mincarttotal': u'3', 'discountstart': u'2014-02-19', 'marketid': None, 'discountitem': [], 'uses': None, 'discountcategory': [], 'owner': None, 'discounttype': u'1', 'discountexpiry': u'3'}
いつものようにそれを助けることができる人に感謝します!