V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
coolrice
V2EX  ›  问与答

SQLAlchemy 的类导入问题

  •  
  •   coolrice · 2015-05-20 19:12:02 +08:00 · 1948 次点击
    这是一个创建于 3260 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我本来是吧所有类放到一个models.py文件下,每个类继承相同的Base,这样在建表的时候直接import models模块
    现在我想把类分开放到不同的文件里,但都放到models包里,但是这样我在from models import * 时就是不能创建表,只能from models import user 这样才行,又没有什么好的方法直接导入全部呢?

    5 条回复    2015-05-21 13:23:28 +08:00
    billlee
        1
    billlee  
       2015-05-20 20:06:26 +08:00
    你的 __init__.py 里设置 __all__ 了吗?
    coolrice
        2
    coolrice  
    OP
       2015-05-20 22:30:30 +08:00
    @billlee 没有
    billlee
        3
    billlee  
       2015-05-20 22:50:57 +08:00
    coolrice
        4
    coolrice  
    OP
       2015-05-21 06:40:58 +08:00
    @billlee 貌似懂了,不过要自己手动写好,有没有方法能得到这个包下的所有模块名字?
    billlee
        5
    billlee  
       2015-05-21 13:23:28 +08:00
    @coolrice find models/ -maxdepth 1 -name *.py -print0 | xargs -0 basename -s .py
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5814 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 06:24 · PVG 14:24 · LAX 23:24 · JFK 02:24
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.