我的Model主键如何以特定数字开头?

时间:2022-02-13 21:52:14

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

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

10001, 10002, 10003, 10004...

10001,10002,10003,10004 ......

My User class:

我的用户类:

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

Is it possible to make it come true?

是否有可能实现它?

EDIT-1

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对象的id值以某个值开始?

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

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

3 个解决方案

#1


7  

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

这种方式与使用RAW_SQL进行datamigrations相同,更改APPNAME:

python manage.py makemigrations APPNAME --empty

inside the created file:

在创建的文件中:

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

#2


1  

The solution is to set autoincrement field like:

解决方案是设置自动增量字段,如:

user_id = models.AutoField(primary_key=True)

After this, you can run this command on the database side. You can run this python command by using signals:

在此之后,您可以在数据库端运行此命令。您可以使用信号运行此python命令:

ALTER SEQUENCE user_id RESTART WITH 10000;

You can do this by different method.

你可以用不同的方法做到这一点。

from django.db.models.signals import post_syncdb
from django.db import connection, transaction
cursor = connection.cursor()
cursor = cursor.execute(""" ALTER SEQUENCE user_id RESTART WITH 10000; """)
transaction.commit_unless_managed()

post_syncdb.connect(auto_increment_start, sender=app_models)

In Django, a model can't have more than one AutoField. And this is used to set a primary key different from the default key.

在Django中,模型不能有多个AutoField。这用于设置与默认密钥不同的主键。

#3


0  

My solution is to do it manually:

我的解决方案是手动完成:

$ ./manage.py shell
Python 3.6.5 (default, Apr  1 2018, 05:46:30)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.4.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from django.contrib.auth.models import User

In [2]: u = User.objects.create_user('name', '', '')

In [3]: User.objects.filter(id=u.id).update(id=10000-1)
Out[3]: 1

In [4]: u.delete()
Out[4]:
(0,
 {'admin.LogEntry': 0,
  'auth.User_groups': 0,
  'auth.User_user_permissions': 0,
  'auth.User': 0})

In [5]: uu = User.objects.create_user('user', '', '')

In [6]: uu.id
Out[6]: 10000

#1


7  

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

这种方式与使用RAW_SQL进行datamigrations相同,更改APPNAME:

python manage.py makemigrations APPNAME --empty

inside the created file:

在创建的文件中:

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

#2


1  

The solution is to set autoincrement field like:

解决方案是设置自动增量字段,如:

user_id = models.AutoField(primary_key=True)

After this, you can run this command on the database side. You can run this python command by using signals:

在此之后,您可以在数据库端运行此命令。您可以使用信号运行此python命令:

ALTER SEQUENCE user_id RESTART WITH 10000;

You can do this by different method.

你可以用不同的方法做到这一点。

from django.db.models.signals import post_syncdb
from django.db import connection, transaction
cursor = connection.cursor()
cursor = cursor.execute(""" ALTER SEQUENCE user_id RESTART WITH 10000; """)
transaction.commit_unless_managed()

post_syncdb.connect(auto_increment_start, sender=app_models)

In Django, a model can't have more than one AutoField. And this is used to set a primary key different from the default key.

在Django中,模型不能有多个AutoField。这用于设置与默认密钥不同的主键。

#3


0  

My solution is to do it manually:

我的解决方案是手动完成:

$ ./manage.py shell
Python 3.6.5 (default, Apr  1 2018, 05:46:30)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.4.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from django.contrib.auth.models import User

In [2]: u = User.objects.create_user('name', '', '')

In [3]: User.objects.filter(id=u.id).update(id=10000-1)
Out[3]: 1

In [4]: u.delete()
Out[4]:
(0,
 {'admin.LogEntry': 0,
  'auth.User_groups': 0,
  'auth.User_user_permissions': 0,
  'auth.User': 0})

In [5]: uu = User.objects.create_user('user', '', '')

In [6]: uu.id
Out[6]: 10000