简体   繁体   English

Django项目models.py与app models.py

[英]Django project models.py versus app models.py

I am learning Django and I am trying to understand the use of models.py in the project versus the application. 我正在学习Django,我正在尝试理解项目中的models.py与应用程序的使用。 It seems from the tutorial examples that I include a model definition in the app, but when I went to apply that knowledge to my own existing database I got stuck. 从教程示例中可以看出,我在应用程序中包含了一个模型定义,但是当我将这些知识应用到我自己的现有数据库时,我遇到了困难。

I took a database that I use (a copy of course) and generated the conceptual schema as a django model using inspectdb. 我使用了一个我使用的数据库(当然是一个副本)并使用inspectdb生成了一个django模型的概念模式。 I did this at the project level, and presumed then I could write apps using subschemas in the applications for that project. 我在项目级别做了这个,并假设我可以使用该项目的应用程序中的子模式编写应用程序。

But generalizing the tutorial, they define the model in the application's model.py. 但是通用教程,他们在应用程序的model.py中定义了模型。 If I did that, I would be repeating the model (or part of it) that's already at the project level, which seems like a mistake and a maintenance issue. 如果我这样做,我会重复已经在项目级别的模型(或部分模型),这似乎是一个错误和维护问题。

So how, in Django style, do I use the project schema (or parts of it) without redefining it in the application's models.py? 那么,在Django风格中,我如何使用项目模式(或部分模式)而无需在应用程序的models.py中重新定义它?

Thanks in advance. 提前致谢。

There shouldn't be any reason to have "project level models" (or "project level views" for that matter). 应该没有任何理由拥有“项目级别模型”(或“项目级别视图”)。 You just need to split the functionality into separate apps. 您只需将功能拆分为单独的应用程序即可。

Let's say you are designing an intranet website for a school. 假设您正在为学校设计Intranet网站。 You would have one app that deals with students' accounts, and another app generating timetables, and yet another one for an internal message board, etc.. Every app defines its own models (there are no "project level models"), but apps can import each others models (so message board posts can have a ForeignKey field pointing at student from the "students" app). 你会有一个应用程序来处理学生的帐户,另一个应用程序生成时间表,而另一个应用程序用于内部留言板等。每个应用程序定义自己的模型(没有“项目级模型”),但应用程序可以导入彼此模型(因此留言板帖子可以有一个指向来自“学生”应用程序的学生的ForeignKey字段)。

See also James Bennett's "writing reusable Django applications" presentation from DjangoCon 2008. 另请参阅James Bennett撰写的 DjangoCon 2008 “可重用Django应用程序”演示文稿

  • A model should only be defined once in a Django project 模型只应在Django项目中定义一次

  • The model needs to exist in an app under the project 该模型需要存在于项目下的应用程序中

  • You can access other app's models by importing them 您可以通过导入来访问其他应用程序的模型

  • If you need to "add on" to an existing model it can be inherited from (see: multi table inheritance ). 如果您需要“添加”到现有模型,则可以继承它(请参阅: 多表继承 )。 This is fairly simple but if you're just starting out you may want to leave that for later. 这很简单但是如果你刚刚开始,你可能想留待以后再做。

Django models can only reside in applications, not in project itself. Django模型只能驻留在应用程序中,而不能驻留在项目本身中。 By default manage.py inspectdb outputs content of the models.py file and it's up to you to put it in the right place. 默认情况下, manage.py inspectdb输出models.py文件的内容,由您决定是否将它放在正确的位置。

In your case it would be easier to put whole thing in one app and later split it in the places where it would make sense. 在你的情况下,将整个东西放在一个应用程序中然后将其拆分到有意义的地方会更容易。

I'm not sure what's the state with the current version, but before presence of models module in package was indication that this is django application and can be put in INSTALLED_APPS list. 我不确定当前版本的状态是什么,但是在包中存在models模块之前表明这是django应用程序并且可以放在INSTALLED_APPS列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM