推荐学习书目
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
holajamc
V2EX  ›  Python

问一问 Django 的分表操作

  •  
  •   holajamc · Dec 21, 2018 · 2527 views
    This topic created in 2700 days ago, the information mentioned may be changed or developed.

    现在有一个问题,数据库每天会创建一张新的表,表名格式大概为:

    'prefix_{}'.format(datetime.datetime.now().strftime("%Y%m%d"))
    

    而现在在开发一个 Django 后端程序,仅仅需要用 django-admin 链接数据库进行一些很基础的信息展示即可。不过现在卡在了怎么才能动态的使用 model 更换数据库表名,拿到当前日期的数据呢?

    7 replies    2018-12-21 16:12:58 +08:00
    NaVient
        1
    NaVient  
       Dec 21, 2018
    既然只是为了展示一些基础数据为什么不直接写 SQL 呢?
    holajamc
        2
    holajamc  
    OP
       Dec 21, 2018
    @NaVient 懒……并且想试试 Django 的这部分功能,外加不知道以后是否会增加需求,综合以上三点没有选 SQL
    NaVient
        3
    NaVient  
       Dec 21, 2018
    @holajamc 试试更改 self.Meta.db_table 呢
    NaVient
        4
    NaVient  
       Dec 21, 2018
    @holajamc 我指的是你模型类的 self
    holajamc
        5
    holajamc  
    OP
       Dec 21, 2018
    virusdefender
        6
    virusdefender  
       Dec 21, 2018   ❤️ 1
    什么数据库啊,可以从数据库层面的 partition 解决问题,使用主表表名,然后自动路由到正确的子表上去。

    https://www.postgresql.org/docs/current/ddl-partitioning.html
    holajamc
        7
    holajamc  
    OP
       Dec 21, 2018
    @virusdefender 唔谢谢大佬回复,用的是 MySQL,我去看看这种方法是否可行~
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3768 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 171ms · UTC 04:45 · PVG 12:45 · LAX 21:45 · JFK 00:45
    ♥ Do have faith in what you're doing.