V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
ansheng
V2EX  ›  Python

如何自定义 Django 主键自增的起始值?

  •  
  •   ansheng ·
    anshengme · 2017-03-09 09:32:01 +08:00 · 2537 次点击
    这是一个创建于 2612 天前的主题,其中的信息可能已经有所发展或是发生改变。

    这是一个很诡异的问题,搜了大半天,貌似得到的答案就是 Django 没有这个功能,问问小伙伴们可有折中的解决方案呢?

    场景如:

    订单的 ID 唯一且从 100000000 开始,自增+1 ,用的是 django 的 model ,不能直接修改数据库。

    glasslion
        1
    glasslion  
       2017-03-09 09:54:38 +08:00
    在 建表的 migration 里加个 ALTER TABLE 的 SQL
    ansheng
        2
    ansheng  
    OP
       2017-03-09 10:13:10 +08:00
    @glasslion 这个确实是可以,但是已经违反了 django model 的定义,所以还是希望只在 model 中进行实现,而不修改数据库和 migration 中的 sql 文件
    wph95
        3
    wph95  
       2017-03-09 11:10:01 +08:00
    我印象里 django (1.4 的源码) 的实现,创建新数据 sql insert 不带 prime key ,
    我自己实现的办法是 改 数据库里这个表的 auto increment 。
    ansheng
        4
    ansheng  
    OP
       2017-03-09 11:11:31 +08:00
    @wph95 可还有其他解决方案呢?
    lgh
        5
    lgh  
       2017-03-09 13:09:27 +08:00 via iPhone
    ansheng
        6
    ansheng  
    OP
       2017-03-09 17:35:01 +08:00
    @lgh 刚用到这个用于 django 的测试,你是要我预先填充这么多数据?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1726 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 00:33 · PVG 08:33 · LAX 17:33 · JFK 20:33
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.