一个应用程序包含多个模型与多个应用程序包含一个模型
我目前正在 Django
中开发自己的博客。但我已经从一开始就陷入困境了。所以,这是我的树层次结构:
/pyroot/nemoden/
|~blog/
| |-__init__.py
| |-admin.py
| |-models.py
| |-tests.py
| `-views.py
|+css/
|+images/
|+js/
|~templates/
| |-index.html
| `-postslist.html
|-__init__.py
|-manage.py
|-settings.py
`-urls.py
我所做的是:创建了名为 blog 的新应用程序,并在 blog/models.py
中描述了博客所需的所有模型(用户、帖子、评论、等),但后来我看了 Jeff Hui
视频 并意识到它这可能是一个坏主意,在 Django-world 中人们不会这样做...我们在... PHP-world
中使用我们的 PHP 框架。我想最好是为标签、评论、用户等区分 Django 应用程序。
所以,我要问的是:
每个 Django 应用程序有一个模型是不是更好?如果是这样,当我不应该为模型创建新的 Django 应用程序时是否存在一些例外?
我想要:
/pyroot/nemoden/
|~blog/ # this is actual application (not a django-application). It uses all the models in views.py, so django-apps becomes just models
| |-__init__.py
| |-tests.py
| `-views.py # all the views (controllers in other frameworks) used by our (well,... my) weblog
|+css/
|+images/
|+js/
|~templates/
| |-index.html
| `-postslist.html
|-__init__.py
|~post/
| |-__init__.py
| |-tests.py
| |-admin.py
| |-models.py # only Post model goes here
| `-views.py
|~tag/
| |-__init__.py
| |-tests.py
| |-admin.py
| |-tag.py # only Tag model goes here
| `-views.py # <---- I don't know why we still need it here!
|-manage.py
|-settings.py
`-urls.py
如你所见,我从 blog
应用程序中删除了 models.py
和 admin.py
,所以现在 blog
应用程序更像应用程序
或主应用程序
(如果您愿意),它使用所有模型(django-apps)并且主要由views.py< /代码>。我认为现在我们不需要所有
django-apps
中的所有 views.py
(不过,这只是一个大问号 - 这只是理论上的) 。
我的方法是否有效,或者我现在可能会遇到看不见的问题?
I'm currently developing my own weblog in Django
. But I've already stucked right in the beginning. So, here is my tree hierarchy:
/pyroot/nemoden/
|~blog/
| |-__init__.py
| |-admin.py
| |-models.py
| |-tests.py
| `-views.py
|+css/
|+images/
|+js/
|~templates/
| |-index.html
| `-postslist.html
|-__init__.py
|-manage.py
|-settings.py
`-urls.py
What I've done is: created new application called blog and described all the models I need for a blog in blog/models.py
(User, Post, Comment, etc.), but then I watched Jeff Hui's
video and realised that it is probably a bad idea and in Django-world
people don't do that... what we do in... PHP-world
using our PHP Frameworks
. I guess it is better to have distinguished Django-applications for Tags, Comments, Users, etc...
So, what I'm asking is:
Is it better to have one model per Django-app? If so, are there some exceptions when I should not create a new Django-app for a model?
I want to go with:
/pyroot/nemoden/
|~blog/ # this is actual application (not a django-application). It uses all the models in views.py, so django-apps becomes just models
| |-__init__.py
| |-tests.py
| `-views.py # all the views (controllers in other frameworks) used by our (well,... my) weblog
|+css/
|+images/
|+js/
|~templates/
| |-index.html
| `-postslist.html
|-__init__.py
|~post/
| |-__init__.py
| |-tests.py
| |-admin.py
| |-models.py # only Post model goes here
| `-views.py
|~tag/
| |-__init__.py
| |-tests.py
| |-admin.py
| |-tag.py # only Tag model goes here
| `-views.py # <---- I don't know why we still need it here!
|-manage.py
|-settings.py
`-urls.py
As you see I cut out models.py
and admin.py
from blog
app, so now blog
app more like the app
or main app
if you wish which uses all the models (django-apps) and mainly consists of views.py
. And I think now we don't need all views.py
in all django-apps
(this one is under a BIG question mark, though - it is just in theory).
Is my approach any good or I will suffer problems invisible for me now, maybe?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
可重用应用程序的关键思想之一是:做一件事,并把它做好
如果一个应用程序需要多个模型(如果是博客应用程序,则为 PostEntry、PostAuthor)这绝不是坏事。然而,标签、类别、评论代表了不同的功能,理想情况下可以在另一个上下文中重用,因此应该作为独立的应用程序分发。
为了了解一个好的应用程序组织,我首先看看 Django 可重用应用程序约定。
然后,您就可以聆听 James Bennett 在 DjangoCon 2008 (幻灯片)。另一个关于同一主题的最新版本是 PyCon 的 Pluggable Django Application Patterns 2011年
One of the key ideas for a reusable application is: Do one thing, and do it well
If an app needs several models (PostEntry, PostAuthor in case of a Blog App) this is by no means bad. Tags, Categories, Comments however represent distinct features which ideally can be reused in another context and therefore should be distributed as standalone apps.
To get a feeling for a good app organization I'd first take look at Django Reusable App Conventions.
Then you are ready for James Bennett's talk about Resuable Apps from DjangoCon 2008 (Slides). Another, more recent take on the same subject is Pluggable Django Application Patterns from PyCon 2011
经验法则是“应用程序”应该是一个完整的功能。如果您的博客在没有标签的情况下无法运行(就像字面意思一样,不仅仅是拥有带有标签的博客比没有标签更好),那么标签应该成为博客应用程序的一部分。
然而,这里没有明确的答案。一些应用程序纯粹主义者完全专注于可重用性,并使每个应用程序成为一个独立的功能块,几乎不依赖于其他任何东西。有些使用单个 Django 应用程序创建整个应用程序。这实际上取决于您来决定在您的特定场景中什么最有意义。
一般来说,我想说的是,将可能不会在其他地方使用但应用程序所需的功能组合起来,全部在同一个应用程序中。标签或评论之类的东西可能是它们自己的应用程序的候选者,事实上,您可以找到许多此类应用程序,只需将它们插入您的应用程序即可提供该功能。
不过,在任何比简单的待办事项列表更复杂的应用程序中,你几乎不可避免地会遇到大量的交叉。没有一个正确答案。只要运用常识并思考 DRY(不要重复自己),你就会做得很好。
The rule of thumb is than an "app" should be a complete piece of functionality. If your blog cannot run without tags (like literally, not just it would be nicer to have a blog with tags than without) then tags should be part of the blog app.
However, there's no clear-cut answer here. Some app-purists focus entirely on re-usability and make each app a discrete piece of functionality with little to no dependencies on anything else. Some create entire applications with a single Django app. It's really up to you to decide what makes the most sense in your particular scenario.
In general, I would say combine functionality that won't likely be used else, but is required for the app, all in the same app. Things like tags or comments are probably candidates for their own apps, and indeed, you can find many such apps available that can be simply plugged into your app to provide that functionality.
In any app more complicated than a simple to-do list, you're pretty much inevitably going to end up with a good deal of crossover, though. There's no one right answer. Just use common sense and think DRY (don't repeat yourself) and you'll do okay.
我在 YouTube 上找到了这个人,他说他已经解决了这个问题:既有一个巨大的应用程序,又有很多他认为不好的小应用程序。
http://youtu.be/URztqq1kiqI?t=22m39s
根据我自己的经验:您不不需要一个大的应用程序,因为人们可以处理更好的文件夹树,这些文件夹树分散一点,但不会太多。拥有一个应用程序会让你更难掌握项目的组成部分(对于新人来说)。
另一方面,你拥有的应用程序越多(相互依赖),你遇到循环导入的机会就越大问题。所以你需要一个策略来避免这些事情。同样,新成员也可能会让项目陷入问题。
总而言之,那些开发过更多项目、时间更长的人通常应该是做出架构决策的人。
I found this guy on youtube, that says he's dealt with this exact problem: both having a huge app and lots of little ones he considers to be not good.
http://youtu.be/URztqq1kiqI?t=22m39s
From my own experience: you don't want one big app, because people can handle better folder trees that are spread a little, but not too much. Also having one app would make harder to grasp what are the components of your project (for new people)
On the other hand, the more apps you have (that do depend on one another), the more chances you have to run into circular import problems. So you need a strategy for avoiding these things. Here also, newer members would tend to drive the project into problems.
All in all, people who've developed on more projects, for longer, should usually be the ones making architectural decisions.
我的主观观点是应用程序应该是可重用的。如果它们确实可重用,您可以在下一个项目中进行 pip 安装。因此,基本上可重用的应用程序应该与您的代码库分开,最好是在 pypi 上。
我每个项目只使用一个应用程序,并使用包来分解模块。
例如。
之前
之后
My subjective view is that apps are meant to be reusable. If they are truly reusable you could pip install on your next project. So basically reusable apps should be separate from your codebase and ideally on pypi.
I just use one app per project and use packages to break up modules.
eg.
Before
After
Nemoden,您更改项目树的方法是正确的。您应该将您的应用程序划分为更小的功能,因为这将使您的应用程序更加模块化,并且代码将来更加可重用。
Nemoden, your approach to change project tree is correct. You should divide your app into smaller functionalities as this will enable you to make your application more modular and code will be more reusable in the future.