Django BigInteger自动增量字段为主键? [英] Django BigInteger auto-increment field as primary key?

查看:773
本文介绍了Django BigInteger自动增量字段为主键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建设一个涉及很多集体智慧的项目。访问网站的每个用户都创建了一个唯一的配置文件,并且他们的数据后来被用于为自己和其他用户计算最佳匹配。



默认情况下,Django创建一个INT 11) id 字段来处理模型的主键。我很担心这是非常快速溢出的(即〜2.4b的设备访问页面,没有预先的cookie设置)。我如何更改它在MySQL和long()里面的Django本身表示为BIGINT?



我发现我可以做以下( http://docs.djangoproject.com/en/dev/ref/models/fields/# bigintegerfield ):

  class MyProfile(models.Model):
id = BigIntegerField(primary_key = True )

但是有没有办法让它自动增量,像往常一样 id 字段?另外,我可以使它无符号,以便我有更多的空间来填写?



谢谢!

解决方案


注意:根据Larry的代码,这个答案被修改了。以前的解决方案扩展fields.BigIntegerField,但更好地扩展fields.AutoField


我有同样的问题,并解决了以下代码:来自django.db.models导入字段的

 
来自south.modelsinspector import add_introspection_rules

class BigAutoField (fields.AutoField):
def db_type(self,connection):
if'mysql'in connection .__ class __.__ module__:
return'bigint AUTO_INCREMENT'
return super(BigAutoField ,self).db_type(connection)

add_introspection_rules([],[^ MYAPP\.fields\.BigAutoField])

显然这对南迁很有效。


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!

解决方案

NOTE: This answer as modified, according to Larry's code. Previous solution extended fields.BigIntegerField, but better to extend fields.AutoField

I had the same problem and solved with following code:

from django.db.models import fields
from south.modelsinspector import add_introspection_rules

class BigAutoField(fields.AutoField):
    def db_type(self, connection):
        if 'mysql' in connection.__class__.__module__:
            return 'bigint AUTO_INCREMENT'
        return super(BigAutoField, self).db_type(connection)

add_introspection_rules([], ["^MYAPP\.fields\.BigAutoField"])

Apparently this is working fine with south migrations.

这篇关于Django BigInteger自动增量字段为主键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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