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
holajamc
V2EX  ›  Python

问一问 Django 的分表操作

  •  
  •   holajamc · 2018-12-21 14:42:49 +08:00 · 2065 次点击
    这是一个创建于 1952 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

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

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

    https://www.postgresql.org/docs/current/ddl-partitioning.html
    holajamc
        7
    holajamc  
    OP
       2018-12-21 16:12:58 +08:00
    @virusdefender 唔谢谢大佬回复,用的是 MySQL,我去看看这种方法是否可行~
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1862 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 16:29 · PVG 00:29 · LAX 09:29 · JFK 12:29
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.