How to store empty value as an Integerfield

后端 未结 3 2078
一整个雨季
一整个雨季 2020-12-24 02:11

I\'ve seen all the similar threads, read the docs, and tried many combinations to store an empty value as IntegerField in db and failed every single time.

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-24 02:56

    You can override 'clean' method in your form class, then cleaning the integer data for avoiding empty string to be submitted, see below example:

    def clean(self):
        self.cleaned_data = super(YourForm, self).clean()
        if self.cleaned_data['age'] == '': self.cleaned_data['age'] = None
        return self.cleaned_data
    

提交回复
热议问题