I am learning Django and I am trying to understand the use of models.py in the project versus the application. 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.
我正在学习Django,我正在尝试理解模型的使用。项目中的py与应用程序。从教程示例中可以看出,我在应用程序中包含了一个模型定义,但当我将这些知识应用到自己现有的数据库时,我遇到了麻烦。
I took a database that I use (a copy of course) and generated the conceptual schema as a django model using inspectdb. I did this at the project level, and presumed then I could write apps using subschemas in the applications for that project.
我使用了一个我使用的数据库(当然是一个副本),并使用inspectdb将概念模式生成为django模型。我在项目级别上做了这个,然后假定我可以在项目的应用程序中使用子模式编写应用程序。
But generalizing the tutorial, they define the model in the application's 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.
但是概括教程,他们在应用程序的model.py中定义模型。如果我这样做,我将重复已经在项目级别上的模型(或它的一部分),这似乎是一个错误和维护问题。
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风格中,我如何使用项目模式(或它的一部分)而不在应用程序的model .py中重新定义它呢?
Thanks in advance.
提前谢谢。
3 个解决方案
#1
22
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. 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).
假设你正在为一所学校设计一个内联网网站。你会有一个处理学生账户的应用,还有一个生成时间表的应用,还有一个用于内部留言板等等。每个应用程序都定义自己的模型(没有“项目级模型”),但是应用程序可以相互导入模型(因此留言板上的帖子可以有一个指向来自“学生”应用程序的学生的外国关键字段)。
See also James Bennett's "writing reusable Django applications" presentation from DjangoCon 2008.
请参见James Bennett在DjangoCon 2008年的“编写可重用的Django应用程序”演示文稿。
#2
5
-
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.
如果您需要“添加”到现有的模型,它可以继承自(参见:多表继承)。这很简单,但如果你刚开始,你可能想把它留到以后。
#3
3
Django models can only reside in applications, not in project itself. 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.
Django模型只能驻留在应用程序中,而不能驻留在项目本身中。默认情况下管理。py检查db输出模型的内容。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.
我不确定当前版本的状态是什么,但是在package中出现models模块之前,这表明这是django应用程序,可以放在INSTALLED_APPS列表中。
#1
22
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. 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).
假设你正在为一所学校设计一个内联网网站。你会有一个处理学生账户的应用,还有一个生成时间表的应用,还有一个用于内部留言板等等。每个应用程序都定义自己的模型(没有“项目级模型”),但是应用程序可以相互导入模型(因此留言板上的帖子可以有一个指向来自“学生”应用程序的学生的外国关键字段)。
See also James Bennett's "writing reusable Django applications" presentation from DjangoCon 2008.
请参见James Bennett在DjangoCon 2008年的“编写可重用的Django应用程序”演示文稿。
#2
5
-
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.
如果您需要“添加”到现有的模型,它可以继承自(参见:多表继承)。这很简单,但如果你刚开始,你可能想把它留到以后。
#3
3
Django models can only reside in applications, not in project itself. 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.
Django模型只能驻留在应用程序中,而不能驻留在项目本身中。默认情况下管理。py检查db输出模型的内容。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.
我不确定当前版本的状态是什么,但是在package中出现models模块之前,这表明这是django应用程序,可以放在INSTALLED_APPS列表中。