gpt4 book ai didi

python - 我的模型主键如何以特定数字开头?

转载 作者:太空狗 更新时间:2023-10-29 20:13:22 32 4
gpt4 key购买 nike

我有一个 User 模型,我希望它的 id 从 10000 开始,然后它的 id 应该像这样自动递增:

10001, 10002, 10003, 10004...

我的用户类:

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

有可能实现吗?

EDIT-1

在问这个问题之前,我已经阅读了这个链接:Is there a way to set the id value of new Django objects to start at a certain value?

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

最佳答案

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

python manage.py makemigrations APPNAME --empty

在创建的文件中:

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

关于python - 我的模型主键如何以特定数字开头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50757235/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com