views:

48

answers:

2

Hello,

I'm trying to store a number in django that looks like this:

000001

My problem is that if I type this inside an IntegerField it gets converted to "1" without the leading zeros. I've tried also with a DecimalField with the same result. How can I store the leading zeros whithout using a CharField? (I need to manipulate that number in it's integer form)

+1  A: 

I think you should use a CharField and do something like that :

try:
    value = int(field)
    # do something with value
except valueError:
    # raise a ValidationError if you are in the *clean* methods of the form
    # or raise an other exception
    # or handle the error
    # or just raise :-)
Ghislain Leveque
Maybe i'm doing something wrong, but I can't do int(models.CharField....) because it tries to convert a CharField instance (not the value) to an int
Oscar Carballal
Arf yes I think you are doing something wrong. Maybe if you show your code I can track it.
Ghislain Leveque
+3  A: 

Don't store it with the leading zeros. Format it on output instead:

(in view: value = 1)
{{ value|stringformat:"04d" }} # displays as 0001
Daniel Roseman
I didn't think about that. It might do the trick, since where I really need the leading zeros is in output, not stored inside the DB.
Oscar Carballal