在 Django 的整数列中存储带有前导零的数据

发布于 2024-10-31 18:47:56 字数 277 浏览 2 评论 0原文

我需要能够将 098532 这样的数字插入到我的表格中。这与这个完全相同但实际上我需要用前导零来存储它,因为我将使用这些数据进行 3rd 方 API 调用。

给出的答案是格式化输出,但这并不能真正解决我的问题。

将列的数据类型更改为 CHAR 字段的最佳解决方案是?

I need to be able to insert numbers like 098532 into my table. This is the exact same question as this but I actually need to store it with the leading zeros as I will be using the data for 3rd party API calls.

The given answer was to format the output which doesn't really solve my problem.

Is the best solution to change the datatype of the column to a CHAR field?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吝吻 2024-11-07 18:47:57

我认为最好的选择是将其作为字符串存储在 charfield 中,然后在需要其他内容时将其转换。

num = "051"
int(num)

如果您需要它作为 int 。您能否提供更多关于 api 期望的信息以及为什么需要前导零?

I think your best bet is to store it as a string in a charfield and then cast it when you need something else.
i.e.

num = "051"
int(num)

if you need it as an int. Could you give a little more information about what the api expects and why you need the leading zeroes?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文