django模型中的大整数场 [英] Big integer field in django models

查看:98
本文介绍了django模型中的大整数场的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单来说:你如何在Django模型中指定一个BIGINT?



在目前的项目中,我正在使用Django(0.97-svn版本),我所有模型中的所有整数字段定义为IntegerField。在开发周期中,我正在使用SQLite进行DB后端的工作。但是,一旦我退出到MySQL,我注意到,对于其中一个IntegerField,MySQL正在截断数据 - 显然,由于我还不知道的原因,SQLite并没有抱怨。我查看了架构,发现Django中的IntegerField在MySQL中被表示为INT(11)字段。自然地,MySQL截断数据的原因是因为数据长度超过了11位数。为了解决方法,我不得不手动更新列,在这种情况下,是一个BIGINT(20)。 Django和MySQL和平共处。但是每当我重置包含该BIGINT的模型的应用程序时,Django会再次将其创建为INT(11)。我看过Django文档,没有发现任何东西。我忽略了一些东西?



谢谢。

解决方案

抱怨。它使用'清单打字',即值具有类型,而不是列。它可以让您将bigtext存储在一个smallint collumn,或任何你想要的! (除了定义一个整数主键,它使用64位整数)。



这是一个非常方便的功能,但它使SQLite成为开发人员的不利选择如果要使用不同的引擎部署。



要使用BIGINT,您必须创建一个自定义字段类。不幸的是,该部分在Django 1.0上已经改变了,所以如果/当你更新,你必须重写它。


In short: How do you specify a BIGINT in Django models?

In a current project I am doing using Django (0.97-svn release), I've all the integer fields in all the models defined as IntegerField. It was working immaculately during the development cycle where I was using SQLite for the DB backend. However, as soon as I rolled off to MySQL, I noticed that for one of the IntegerFields, MySQL was truncating data---apparently, for reasons still unknown to me, SQLite didn't complain. I looked at the schema and found out that an IntegerField in Django is represented as an INT(11) field in MySQL. Naturally, the reason MySQL was truncating data was because the data was more than 11-digit in length. To workaround, I had to manually update the column to, in this case, be a BIGINT(20). Django and MySQL coexist peacefully with that. But whenever I reset the application in which rests the model containing that BIGINT, Django again creates it as an INT(11). I've looked at Django docs, and not found anything. Have I overlooked something?

Thanks.

解决方案

SQLite won't complain ever. it uses 'manifest typing', that is, the values have type, not the columns. It lets you store bigtext on a smallint collumn, or whatever you want! (except if you define an integer primary key, where it uses a 64-bit integer).

that's a very convenient feature, but it makes a SQLite bad choice for developing if you're going to deploy with a different engine.

for using a BIGINT, you'd have to create a custom field class. unfortunately, that part has changed on Django 1.0, so you'd have to rewrite it if/when you update.

这篇关于django模型中的大整数场的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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