我目前正在构建一个涉及大量集体智慧的项目。每个访问网站的用户都会创建一个独特的个人资料,他们的数据随后用于计算自己和其他用户的最佳匹配。
默认情况下,Django 创建一个 INT(11) id 字段来处理模型主键。我担心这种情况会很快溢出(即~2.4b 设备在没有事先设置 cookie 的情况下访问该页面)。如何将其更改为在 MySQL 中表示为 BIGINT 并在 Django 本身中表示为 long() ?
我发现我可以执行以下操作(http://docs. djangoproject.com/en/dev/ref/models/fields/#bigintegerfield):
class MyProfile(models.Model):
id = BigIntegerField(primary_key=True)
但是有没有办法让它自动增量,就像通常的 id 字段一样?另外,我可以将其设为未签名,以便有更多空间来填写吗?
谢谢!
I'm currently building a project which involves a lot of collective intelligence. Every user visiting the web site gets created a unique profile and their data is later used to calculate best matches for themselves and other users.
By default, Django creates an INT(11) id
field to handle models primary keys. I'm concerned with this being overflown very quickly (i.e. ~2.4b devices visiting the page without prior cookie set up). How can I change it to be represented as BIGINT in MySQL and long() inside Django itself?
I've found I could do the following (http://docs.djangoproject.com/en/dev/ref/models/fields/#bigintegerfield):
class MyProfile(models.Model):
id = BigIntegerField(primary_key=True)
But is there a way to make it autoincrement, like usual id
fields? Additionally, can I make it unsigned so that I get more space to fill in?
Thanks!
发布评论
评论(7)
如果您使用 Django 1.10,Django 现在内置了 BigAutoField:
https: //docs.djangoproject.com/en/1.10/ref/models/fields/#bigautofield
Django now has a BigAutoField built in if you are using Django 1.10:
https://docs.djangoproject.com/en/1.10/ref/models/fields/#bigautofield
受到 lfagundes 的启发,但有一个小但重要的更正:
注意,我不是扩展 BigIntegerField,而是扩展 AutoField。这是一个重要的区别。使用 AutoField,Django 将从数据库中检索自动递增的 id,而 BigInteger 则不会。
从 BigIntegerField 更改为 AutoField 时的一个问题是将数据转换为 AutoField 中的 int。
来自 Django 的 AutoField 的通知:
事实
证明这是可以的,正如在 python shell 中验证的那样:
换句话说,转换为 int 不会截断数字,也不会更改基础类型。
Inspired by lfagundes but with a small but important correction:
Notice instead of extending BigIntegerField, I am extending AutoField. This is an important distinction. With AutoField, Django will retrieve the AUTO INCREMENTed id from the database, whereas BigInteger will not.
One concern when changing from BigIntegerField to AutoField was the casting of the data to an int in AutoField.
Notice from Django's AutoField:
and
It turns out this is OK, as verified in a python shell:
In other words, casting to an int will not truncate the number, nor will it change the underlying type.
我遇到了同样的问题,并使用以下代码解决了:
显然,这对于南方迁移来说工作得很好。
I had the same problem and solved with following code:
Apparently this is working fine with south migrations.
从 Django 3.2 开始,可以使用 DEFAULT_AUTO_FIELD 设置(文档)。因此,不再需要覆盖所有模型中的主键。
请注意,从 Django 3.2 开始,生成的新项目的
DEFAULT_AUTO_FIELD
设置为BigAutoField
(发行说明)。Since Django 3.2 the type of implicit primary key can be controlled with the
DEFAULT_AUTO_FIELD
setting (documentation). So, there is no need anymore to override primary keys in all your models.Note that starting with Django 3.2 new projects are generated with
DEFAULT_AUTO_FIELD
set toBigAutoField
(release notes).您可以稍后更改该表。这可能是一个更好的解决方案。
You could alter the table afterwards. That may be a better solution.
如前所述,您可以事后更改该表。这是一个很好的解决方案。
为了不忘记这一点,您可以在应用程序包下创建一个管理模块并使用 post_syncdb 信号。
https://docs.djangoproject.com/en/dev/ref/ Signals/#post-syncdb
这可能会导致 django-admin.py 刷新失败。但这仍然是我所知道的最好的选择。
As stated before you could alter the table afterwards. That is a good solution.
To do that without forgetting, you can create a management module under your application package and use the post_syncdb signal.
https://docs.djangoproject.com/en/dev/ref/signals/#post-syncdb
This can cause django-admin.py flush to fail. But it is still the best alternative I know.
我也有同样的问题。看起来 django 中不支持 BigInteger 自动字段。
我尝试创建一些自定义字段 BigIntegerAutoField 但我遇到了南迁移系统的问题(南无法为我的字段创建序列)。
在尝试了几种不同的方法之后,我决定遵循 Matthew 的建议并进行更改表(例如 postgre 中的 ALTER TABLE table_name ALTER COLUMN id TYPE bigint;) 如果
有 django 支持的解决方案(例如建于 BigIntegerAutoField) 和南部。
I also had the same problem. Looks like there is no support for BigInteger auto fields in django.
I've tried to create some custom field BigIntegerAutoField but I faced a problem with south migration system (south couldn't create sequence for my field).
After giving a try couple of different approaches I decided to follow Matthew's advice and do alter table (e.g.
ALTER TABLE table_name ALTER COLUMN id TYPE bigint;
in postgre)Would be great to have solution supported by django (like built in BigIntegerAutoField) and south.