V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · Mar 9, 2017 · 2984 views
    This topic created in 3346 days ago, the information mentioned may be changed or developed.

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

    场景如:

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

    glasslion
        1
    glasslion  
       Mar 9, 2017
    在 建表的 migration 里加个 ALTER TABLE 的 SQL
    ansheng
        2
    ansheng  
    OP
       Mar 9, 2017
    @glasslion 这个确实是可以,但是已经违反了 django model 的定义,所以还是希望只在 model 中进行实现,而不修改数据库和 migration 中的 sql 文件
    wph95
        3
    wph95  
       Mar 9, 2017
    我印象里 django (1.4 的源码) 的实现,创建新数据 sql insert 不带 prime key ,
    我自己实现的办法是 改 数据库里这个表的 auto increment 。
    ansheng
        4
    ansheng  
    OP
       Mar 9, 2017
    @wph95 可还有其他解决方案呢?
    lgh
        5
    lgh  
       Mar 9, 2017 via iPhone
    ansheng
        6
    ansheng  
    OP
       Mar 9, 2017
    @lgh 刚用到这个用于 django 的测试,你是要我预先填充这么多数据?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   937 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 20:05 · PVG 04:05 · LAX 13:05 · JFK 16:05
    ♥ Do have faith in what you're doing.