如何在 Django 中存储以零开头的整数 [英] How to store an integer leaded by zeros in django

查看:23
本文介绍了如何在 Django 中存储以零开头的整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 django 中存储一个如下所示的数字:

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

000001

我的问题是,如果我在 IntegerField 中键入它,它会被转换为1"而没有前导零.我也尝试过使用 DecimalField 获得相同的结果.如何在不使用 CharField 的情况下存储前导零?(我需要以整数形式操作该数字)

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)

推荐答案

不要用前导零存储它.改为在输出时对其进行格式化:

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

(in view: value = 1)
{{ value|stringformat:"04d" }} # displays as 0001

这篇关于如何在 Django 中存储以零开头的整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆