简体   繁体   中英

Django Form some field always get None in clean_field() or clean()

It's always get None in clean() or clean_field(), but it's in self.data.

django 2.x

The fields declaration is:

    phone = forms.CharField(
        label='',
        min_length=phone_number_min,
        max_length=phone_number_max,
    )

    code_length = settings.SMS_CODE_LENGTH
    code = forms.CharField(
        label='',
        min_length=code_length,
        max_length=code_length,
    )
    def clean_code(self):
        code = self.cleaned_data.get('code')
        phone = self.cleaned_data.get('phone')
        result, message = sms_validator.validate(phone, code)

    def clean(self):
        code = self.cleaned_data.get('code')
        phone = self.cleaned_data.get('phone')
        result, message = sms_validator.validate(phone, code)

Both of above all run in error:

phone = None

But if

phone = self.data.get('phone')

It's can get the value.

I want to get the phone value in clean_data

Firstly, you must always return the cleaned value from a field clean method. Secondly, it is not safe to access other field values in a field clean method; that is what the overall clean() method is for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM