How to organize Django project with abstract models
I have a few models: 'Article, Video, BlogPost, News, Commodity'. Each are in their own application.
They all are basically the same models with a few extra fields on each. But each share about 15 fields. I'm using an abstract base class. I'm trying to figure out how I should do organization for this. My current setup is like this:
apps/
abstract_models.py
abstract_templateta开发者_运维知识库gs.py
abstract_forms.py
articles/
models.py
...
videos/
models.py
...
blogs/
...
While I know this isnt a good way, I'm just not sure where to put all the information that is shared. I've been doing like this, then per app just subclassing the Form or the Model and making the local modifications. Since they are just a small amount of changes vs the whole picture I think that abstract class is the way to go, but I may be wrong.
They share so much structure, but for obvious reasons I would like to keep them separate apps. But I would like to clean it up a bit.
Any thoughts would be greatly appreciated.
I setup an app that I can use across different projects and call it tools
. In tools
I have my basic base models that I tend to reuse across projects and import it where needed.
For example, I have a CreatedModifiedModel
in tools/models.py
which adds fields for creation and modification time, as well as the user who did the creating and modifying.
After being defined once, I can simply do:
from tools.models import CreatedModifiedModel
class Widget(CreatedModifiedModel):
# comes with my four fields automatically
You could create a single app called base
or core
or tools
and then put all your abstract classes in there, to help keep it clean and make it reusable in the future.
The Pinax project have something similar with groups. They have made a their base class and the ones that extend it into an application.
/apps
/group
base.py
...
/projects
models.py
...
That seems like a great to organize it. You can take a look at their source code at github.
At my firm, we typically organize all of the abstract components of my Django projects into an app called common
. It is structured as a normal Django app, except that it has no urls, and no concrete models. It gets installed in settings.py
, just like any other app.
It's also a great place to stick any globally-used mixins or exceptions.
my_prjoject/
common/
models.py <- abstract models only
templates/
common/
base.html
views.py
mixins.py
exceptions.py
article/
models.py
views.py
urls.py
templates/
atricle/
detail.html
...
Note: name your apps in the singular. If you don't birds will attack you.
With this app in place, I can reference it from any other app, such as in:
article/models.py:
from common import models as common_models
class Article(common_models.BaseModel):
additional_field = models.CharField(max_length=10) # Whatever
article/views.py:
from common import views as common_views
class Detail(common_views.DetailBase):
template_name = "article/detail.html"
atricle/templates/article/detail.html:
{% extends 'common/base.html' %}
{% block base %}
<h1>If you downvote this answer, I will fight you</h1>
{% endblock base%}
精彩评论