具有多个模型的一个应用程序与具有单个模型的多个应用程序 [英] One app with many models vs. many apps with single model

查看:35
本文介绍了具有多个模型的一个应用程序与具有单个模型的多个应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在Django 中开发我自己的博客.但我一开始就已经卡住了.所以,这是我的树层次结构:

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

我所做的是:创建了一个名为 blog 的新应用程序,并在 blog/models.py 中描述了一个博客所需的所有模型(用户、帖子、评论等),但是然后我看了Jeff Hui's 视频并意识到这可能是一个坏主意,并且在 Django-world 中人们不会那样做...我们在... PHP-world 中使用我们的 <代码>PHP 框架.我想最好为标签、评论、用户等提供杰出的 Django 应用程序......

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...

所以,我要问的是:

每个 Django 应用程序有一个模型会更好吗?如果是这样,当我不应该为模型创建新的 Django 应用程序时,是否有一些例外?

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?

我想一起去:

/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.pyadmin.py,所以现在是 blog app 更像是 the appmain app 如果你愿意,它使用所有模型(django-apps)并且主要由 views.py 组成.而且我认为现在我们不需要所有 django-apps 中的所有 views.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?

推荐答案

每个人有一个模型更好吗?Django 应用程序?

Is it better to have one model per Django-app?

可重用应用程序的关键思想之一是:做一件事,做好

One of the key ideas for a reusable application is: Do one thing, and do it well

如果一个应用需要多个模型(PostEntry、PostAuthor,如果是博客应用),这绝不是坏事.然而,标签、类别、评论代表不同的功能,理想情况下可以在其他上下文中重复使用,因此应作为独立应用分发.

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.

是否有最佳实践?

要了解一个好的应用程序组织,我首先要看看 Django 可重用应用程序约定.

To get a feeling for a good app organization I'd first take look at Django Reusable App Conventions.

那么您已经准备好迎接 James Bennett 在 DjangoCon 2008 (幻灯片).另一个关于同一主题的最新研究是来自 PyCon 的 Pluggable Django Application Patterns2011

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

这篇关于具有多个模型的一个应用程序与具有单个模型的多个应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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