在 Django 应用程序之间共享模型 [英] Sharing models between Django apps

查看:26
本文介绍了在 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

推荐答案

如果您正在构建一个永远不会向公众发布的内部应用程序,当然,做任何您想做的事情.

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.

如果您正在构建一个公开发布的应用程序,请尝试使其独立(并且依赖于 django 内部,即,尽可能使用 django 提供的内容).如果您确实需要第三方应用程序来运行,或者第三方应用程序将使您的代码更易于管理,那么当然要包含依赖项,但要加倍确保记录所有要求和必要的设置.

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.

但是,我确实不得不质疑制作自己的 User 模型是否与 django 的内置 auth.User 同名.

I do, however, have to question the sanity of making your own User model that has the same name as django's builtin auth.User.

这篇关于在 Django 应用程序之间共享模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆