Becareful using int() or Decimal() for validation storing in Charfield Django


So, we have CharField() as model field in Django. Then we want to validate input into number only.
Usually, we using obj.is_digit() to validate.

Common mistake happen when we using int() to convert string input.

1
2
value = "0123123123"
int(value)

Will return

1
"123123123"

So becareful when using int() or Decimal() for converting value with leading zero


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.