Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
987 views
in Technique[技术] by (71.8m points)

django - One app with many models vs. many apps with single model

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...