我的Model主键如何以特定的数字开头? [英] How can my Model primary key start with a specific number?

查看:75
本文介绍了我的Model主键如何以特定的数字开头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户模型,我希望其ID从 10000 开始,然后其ID应该自动递增:

I have a User model, I want its id start from 10000, then its id should auto-increment like:

10001 10002 10003 10004 ...

我的用户类别:

class User(AbstractUser):
    username = models.CharField(max_length=64)
    ...

有可能实现吗?

EDIT-1

在问这个问题之前,我已阅读以下链接:是否有办法将新Django对象的id值设置为以某个值开始?

Before ask this question, I have read this link:Is there a way to set the id value of new Django objects to start at a certain value?

但是我认为答案不是很好,所以我的意思是,如果在Django中有实现此目的的配置?

But I don't think the answers are good, so I mean if in Django there is a configuration for achieve this?

推荐答案

方法与使用RAW_SQL进行数据迁移,更改AP的方法相同您的PNAME:

the way is the same as to do datamigrations with RAW_SQL, change APPNAME on your:

python manage.py makemigrations APPNAME --empty

在创建的文件内:

operations = [
    migrations.RunSQL(
        'ALTER SEQUENCE APPNAME_USER_id_seq RESTART WITH 10000;'
    )
]

这篇关于我的Model主键如何以特定的数字开头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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