djangodjango-modelsproject-organization

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


Solution

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