python 1 个 django 应用程序中约有 20 个模型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/859192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
About 20 models in 1 django app
提问by Teifion
I have started work on a local app for myself that runs through the browser. Having recently gone through the django tutorial I'm thinking that it might be better to use django rather than just plain python.
我已经开始为自己开发一个通过浏览器运行的本地应用程序。最近浏览了 django 教程后,我认为使用 django 而不是简单的 python 可能更好。
There's one problem: I have at least 20 models and each will have many functions. Quite simply it's going to create one huge models file and probably huge views too. How do I split them up?
有一个问题:我至少有 20 个模型,每个模型都有很多功能。很简单,它会创建一个巨大的模型文件,也可能是巨大的视图。我该如何拆分它们?
The models are all relatedso I can't simply make them into separate apps can I?
这些模型都是相关的,所以我不能简单地将它们制作成单独的应用程序,对吗?
采纳答案by S.Lott
"I have at least 20 models" -- this is probably more than one Django "app" and is more like a Django "project" with several small "apps"
“我至少有 20 个模型”——这可能不仅仅是一个 Django“应用程序”,更像是一个带有几个小“应用程序”的 Django“项目”
I like to partition things around topics or subject areas that have a few (1 to 5) models. This becomes a Django "app" -- and is the useful unit of reusability.
我喜欢围绕具有几个(1 到 5 个)模型的主题或主题领域来划分事物。这变成了一个 Django“应用程序”——并且是可重用性的有用单元。
The overall "project" is a collection of apps that presents the integrated thing built up of separate pieces.
整个“项目”是一组应用程序,展示了由不同部分组成的集成事物。
This also helps for project management since each "app" can become a sprint with a release at th end.
这也有助于项目管理,因为每个“应用程序”都可以成为最终发布的冲刺。
回答by Jarret Hardie
This is a pretty common need... I can't imagine wading through a models.py file that's 10,000 lines long :-)
这是一个非常普遍的需求......我无法想象通过一个 10,000 行长的 models.py 文件:-)
You can split up the models.py
file (and views.py too) into a pacakge. In this case, your project tree will look like:
您可以将models.py
文件(以及 views.py)拆分为一个包。在这种情况下,您的项目树将如下所示:
/my_proj
/myapp
/models
__init__.py
person.py
The __init__.py
file makes the folder into a package. The only gotcha is to be sure to define an inner Meta
class for your models that indicate the app_label for the model, otherwise Django will have trouble building your schema:
该__init__.py
文件使文件夹成为一个包。唯一的问题是确保Meta
为您的模型定义一个内部类,指示模型的 app_label,否则 Django 将无法构建您的架构:
class Person(models.Model):
name = models.CharField(max_length=128)
class Meta:
app_label = 'myapp'
Once that's done, import the model in your __init__.py
file so that Django and sync db will find it:
完成后,将模型导入您的__init__.py
文件中,以便 Django 和同步数据库找到它:
from person import Person
This way you can still do from myapp.models import Person
这样你仍然可以做 from myapp.models import Person
回答by NathanD
The models are all related so I cant's simply make them into separate apps can I?
这些模型都是相关的,所以我不能简单地将它们分成单独的应用程序,我可以吗?
You canseparate them into separate apps. To use a model in one app from another app you just import it in the same way you would import django.contrib apps.
您可以将它们分成单独的应用程序。要在一个应用程序中使用另一个应用程序中的模型,您只需以导入 django.contrib 应用程序的相同方式导入它。
回答by Flávio Amieiro
Having 20 models in one app might be a sign that you should break it up in smaller ones.
在一个应用程序中包含 20 个模型可能表明您应该将其分解为较小的模型。
The purpose of a Django app is to have a small single-purpose piece of code, that fits nicelly together.
Django 应用程序的目的是拥有一小段单一用途的代码,可以很好地结合在一起。
So, if you had a e-commerce site, you might have a shopping_cart app, a billing app, and so on.
因此,如果您有一个电子商务网站,您可能有一个购物车应用程序、一个计费应用程序等等。
Keep in mind that there is really no problem in apps depending on each other (although it's always better if they can be decoupled), but you should not have an app doing two very distinct things.
请记住,相互依赖的应用程序确实没有问题(尽管如果它们可以解耦总是更好),但是您不应该让应用程序做两件截然不同的事情。
The article Django tips: laying out an applicationmight help you. As always, take everything you read with a grain of salt (including this answer).
文章的Django提示:布局应用程序可以帮助你。与往常一样,对您阅读的所有内容持保留态度(包括这个答案)。
回答by Albinofrenchy
You can break up the models over multiple files. This goes for views as well.
您可以将模型分解为多个文件。这也适用于视图。
回答by Oli
You cansplit them into separate files and simply have imports at the top of your main models.py field.
您可以将它们拆分为单独的文件,只需在主 models.py 字段的顶部导入即可。
Whether you'd really wantto is another question.
你是否真的想要是另一个问题。