[django]让 BigIntegerField 字段自增长
默认的 id 字段是 IntegerField 属性,长度是 11。
现在要将它改为 BigIntegerField 属性,因为它的长度是更长。
class Foo(models.Model): id = models.BigIntegerField(primary_key=True) #...
更改后的数据库字段信息:
mysql> desc foo_bar; +--------------+------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------------+------------+------+-----+---------+-------+ | id | bigint(20) | NO | PRI | NULL | |
问题出来了,更改后的 id 没有自增长的功能。
解决办法就是自定义一个字段属性:
代码结构:
foo # app
|-fields.py
|-models.py
代码:
# fields ...