Django 表名称

发布于 2025-01-06 21:31:34 字数 614 浏览 2 评论 0原文

我正在重构 Django 应用程序。具体来说,我有一个带有大 models.py 文件的应用程序,我试图将其拆分为一堆小文件,例如

myapp/
    models/
        __init__.py
        somemodels.py
        someothers.py
        somemore.py
        ...

models/__init__.py 中的 和从所有其他文件导入所有模型,这样我就不必更改客户端代码。

问题是 Django 现在抱怨表名。模型 Foo 的表曾经是 myapp_foo,但 Django 现在似乎会查找表 myapp.models_foo。也就是说,它似乎使用定义模型的包而不是其应用程序作为前缀(当然 myapp.models 没有注册为 Django 应用程序)。

我知道我可以手动设置每个模型的表名称,但是有没有办法避免这种情况并告诉 Django 这些模型实际上是 myapp 的一部分?

I am refactoring a Django application. Specifically, I have an application with a big models.py file and I am trying to split it into a bunch of small files, like

myapp/
    models/
        __init__.py
        somemodels.py
        someothers.py
        somemore.py
        ...

and in models/__init__.py I import all models from all other files so that I do not have to change client code.

The problem is that Django now complains about table names. Table for model Foo used to be myapp_foo, but it seems that Django now looks for a table myapp.models_foo. That is, it seems that it uses as prefix the package where the models are defined instead of their application (of course myapp.models is not registered as a Django application).

I know I could manually set the table name for each and every models, but is there a way to avoid this and tell Django that these models are actually part of myapp?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文