2014-02-11 1 views
0

좋아요, 그래서 필드의 값에 따라 특정 필드를 채울 필요가 있습니다. 그러나 "discountpercentage"라는 키에 대한 키 오류가 발생합니다. blank no where insed the cleaned_data 어디서 커스텀 클린을 실행하려고하면 키 에러가 발생합니다. 여기 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 

그리고

는 '1'내가 discounttype으로 cleaned_data를 인쇄하면 내가 == 무엇을 얻을하고 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'} 

항상 도움이되는 사람에게 감사드립니다.

답변

0

필드가 채워지지 않으면 cleaned_data에없는 것입니다. 값을 확인하기보다는 키의 존재 여부를 확인해야합니다.

if data['discounton'] == '1' and 'discountitem' not in data: 
+0

감사합니다. 이상한 것은 그것이 aparently하기 전에 내가 지금 내가 결코 이해하지 못했던 나의 주문의 정화가 잡을 수있을 정도로 필요하지 않은 들판을 모두 만들어야 할 필요가있다. 그러나 어느 쪽의 방법이라도 그렇게 일하면서 매우 감사한다! –

+0

나는 그것을 실제로 고치고 그것을 어떻게 이전의 방법으로 되돌려 놓았는지, 나는 당신의 고침을 보았을 때 그것이 내가 무엇을 바꾸 었는지를 보여 주었다. 필자는 틀린 필드를 필요로했습니다. = False, 그리고 깨끗한 방법 어떻게 완벽하게 작동 했나요? 다시 한 번 감사드립니다. –