How can my Model primary key start with a specific number?

后端 未结 3 1894
耶瑟儿~
耶瑟儿~ 2020-12-17 19:50

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

10001, 10002, 10003,

3条回答
  •  误落风尘
    2020-12-17 20:08

    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
    

提交回复
热议问题