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

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

问题描述

我正在开发我自己的 Django 中的博客。但是我一开始就已经坚持下去了。所以,这是我的树层次结构:

  / pyroot / nemoden / 
|〜blog /
| | -__ init__.py
| | -admin.py
| | -models.py
| | -tests.py
| `-views.py
| + css /
| +图像/
| + js /
|〜templates /
| | -index.html
| `-postslist.html
| -__ init__.py
| -manage.py
| -settings.py
`-urls.py

我所做的是:创建新的应用程序,称为博客,并描述了我需要的所有模型,在博客/ models.py (用户,发布,评论等),但后来我看了 Jeff Hui的 视频,并意识到这可能是一个坏主意,在 Django-world 人们不这样做...我们在...中执行... PHP-world 使用我们的 PHP框架。我想这是更好的区分Django应用程序的标签,评论,用户等...



所以,我问的是:



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



我想去:

  / pyroot / nemoden / 
|〜blog /#这是实际应用程序(不是django应用程序)。它使用views.py中的所有模型,所以django-apps变成只是模型
| | -__ init__.py
| | -tests.py
| `-views.py#我们的所有视图(其他框架中的控制器)(我的)weblog
| + css /
| + images /
| + js /
|〜templates /
| | -index.html
| `-postslist.html
| -__ init__.py
|〜post /
| | -__ init__.py
| | -tests.py
| | -admin.py
| | -models.py#只有Post模型在这里
| `-views.py
|〜tag /
| | -__ init__.py
| | -tests.py
| | -admin.py
| | -tag.py#only标签模型在这里
| `-views.py#< ----我不知道为什么我们还需要它!
| -manage.py
| -settings.py
`-urls.py

如你所见,我从<$ c $中删除了 models.py admin.py c> blog 应用程序,所以现在博客应用程序更像是应用程序 main app 如果你愿意使用所有的模型(django-apps),主要由 views.py 组成。而且我认为现在我们并不需要所有 django-apps 中的所有 views.py (这一个在可以吗?



我的方法是否有好处,或者我现在会遇到难以置信的问题吗? b
$ b

如果我的问题不够清楚,我很抱歉,如果是这样,请在下面评论,我将进行修改,以使其更加明了。



有最好的做法吗?



谢谢。

解决方案


最好每个
Django-app有一个模型吗?


可重复使用的应用程序的一个关键思想是:做一件事,做得好



如果应用程序需要几个模型(PostEntry,PostAuthor在博客应用程序的情况下)这绝对不是坏的。标签,类别,评论代表不同的功能,理想情况下可以在另一个上下文中重用,因此应该作为独立应用程序分发。


是否最好的做法?


为了获得一个好的应用程序组织的感觉,我先来看看 Django可重复使用的应用程式公约



然后你准备好詹姆斯·班内特谈谈DjangoCon 2008中的可重复应用程序幻灯片)。另一个更新近的同一主题是来自PyCon的可插拔Django应用程序模式 2011


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 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, is there some exceptions when I should not create 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 cutted 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 consist 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?

I'm sorry if my question is not clear enough, if so, please, comment it below, I will edit it to make it more understood.

Is there best practices?

Thank you.

解决方案

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

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.

Is there best practices?

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的。一个应用程序与许多模型与许多应用程序与单一模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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