0
好吧,我有一個表格,其中取決於下拉的值只有某些領域需要填寫,但即時獲得一個關鍵的錯誤稱爲「discountpercentage」,因爲如果我離開它空白它沒有在哪裏看到insive clean_data,然後當自定義清理trys運行我得到的關鍵錯誤,因爲它無法找到它。Django自定義表單驗證/清除
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打印cleaned_data ==「1」和discountpercentage留空。
{'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'}
感謝任何能夠幫助它的人,這意味着我們永遠都是這樣!
謝謝你的工作!奇怪的是,它現在是純粹的工作,我需要使他們的領域都不需要,所以我的自定義清理可以接管我瞭解,但這從來沒有發生過。但無論哪種方式,它的工作非常感謝你! –
我實際上設法解決它,並返回它之前的情況,因爲當我看着你的修復程序時,它向我展示了我改變了什麼。我需要的字段是必需的=假,然後我的乾淨的方法是如何完美的:)再次感謝。 –