在 Django 应用程序之间共享模型
我简单说一下:为了本着 Django 应用程序的精神和理念工作,应用程序可以从另一个应用程序内部导入模型吗?比如说,用户统计应用程序将从用户应用程序导入模型,例如: from users.models import users
I will be brief: to work in the spirit and idea of a Django app, it is ok for an app to import models from inside another app ? Say, a User statistics app will import models from a User app something like: from users.models import users
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
答案是肯定的。 django 项目中的一个应用程序从另一个应用程序导入模型是完全可以的。 django 项目的力量在于应用程序及其交互。
还要确保您有实用程序应用程序从更通用的应用程序导入模型,而不是相反。因此,“userstatistics”应用程序应该从“users”应用程序导入模型,但“users”应用程序不应依赖于“userstatistics”。
如果您的应用程序从第三方应用程序(例如 django-piston)导入模型,请务必在需求文件中指定。
The answer is yes. It's perfectly okay for one application inside your django project to import models from another application. The power of a django project lies in the apps and their interactions.
Also make sure that you have utility applications importing models from more generic applications and not the other way. So "userstatistics" app should import models from the "users" app but "users" app should not rely on the "userstatistics".
If your app is importing models from a 3rd party application (lets say django-piston), be sure to specify that in a requirements file.
如果您正在构建一个没有机会向公众发布的内部应用程序,当然,可以做任何您想做的事。
如果您正在构建一个几乎不可能向公众发布的内部应用程序,但可能会被未来/当前的开发人员使用,当然,但一定要记录该应用程序正常工作所需的内容。
如果您正在构建一个公开发布的应用程序,请尝试使其保持独立(以及 django 内部依赖,即,尽可能使用 django 提供的内容)。如果您确实需要第三方应用程序才能工作,或者第三方应用程序将使您的代码更易于管理,那么当然要包含依赖项,但请务必记录所有要求和必要的设置。
在大多数情况下,只要有足够的文档,您几乎可以做任何您想做的事情。
然而,我确实不得不质疑创建与 django 内置
auth.User
同名的自己的User
模型是否合理。If you're building an internal app that has no chance of ever being released to the public, sure, do whatever you want.
If you're building an internal app that has little chance of ever being released to the public, but will possibly be used by future/current developers, sure, but be sure to document what the app needs to work properly.
If you're building an app for public release, try to keep it self-dependent (and django-internals dependent, ie, use what django provides, when possible). If you really need a third-party app to work, or if a third party app would make your code more manageable, then sure, include dependencies, but be doubly sure to document all requirements and necessary setup.
In most cases, you can do almost whatever you want so long as you have sufficient documentation.
I do, however, have to question the sanity of making your own
User
model that has the same name as django's builtinauth.User
.您可以尝试更好的 扩展 Django 用户具有继承的模型。您将使用添加了自定义字段的 django 用户,因此所有应用程序都将使用相同的用户。
You cand try better extending the Django User model with inheritance. You will use the django user with custom field added, so you will have the same user for all the apps.
您可以直接在app2/models.py中导入模型。通常你可能需要一个外键,看起来像
You can directly import models in app2/models.py. Usually you might need a foreign key, which looks like
不要这样做。它们将具有相同的应用程序名称,ORM 将会感到困惑。使用抽象模型来代替,并且两者都派生自它。
Don't do this. They will have the same app name and the ORM will be confused. Use an abstract model instead, and have both derive from it.