33π
I prefer to keep my models in models.py and managers in managers.py (forms in forms.py) all within the same app. For more generic managers, I prefer to keep them in core.managers if they can be re-used for other apps. In some of our larger apps with models/modelname.py that will contains a manager and the model code which doesnβt seem bad.
12π
Your best bet with a large set of models is to use django modules to your advantage, and simply create a folder named models. Move your old models.py into this models folder, and rename it __init__.py
. This will allow you to then separate each model into more specific files inside of this model folder.
You would then only need to import each model into your __init__.py
βs namespace.
So, for instance, you might want to separate it into:
yourapp/
models/
__init__.py # This file should import anything from your other files in this directory
basic.py # Just an example name
morespecificmodels.py # Just an example name
managers.py # Might want to separate your manager into this
Then your __init__.py
can just be:
from basic import * # You should replace * with each models name, most likely.
from managers import YourManager # Whatever your manager is called.
This is the structure that I use when my model files get huge, however I try to separate things into more pluggable apps as often as possible β so this is rarely used by me.
Hope this helps.
- [Django]-Django: from django.urls import reverse; ImportError: No module named urls
- [Django]-Django and client-side javascript templates
- [Django]-Django: AppRegistryNotReady()
9π
I always place mine in managers.py. If you have a circular import issue remember that a) You can reference the model class for a manager at self.model, and b) You can do imports inside of functions.
- [Django]-Django: Querying read-only view with no primary key
- [Django]-'collectstatic' command fails when WhiteNoise is enabled
- [Django]-ImportError: Failed to import test module:
4π
What I did when building Django apps was to create a [modelname].py file with just the specific model code, manager code and sometimes form code and used an __init__.py file to import then all in the models directory. This helped me atleast in keeping it managable.
- [Django]-Django templates: forloop.first and forloop.last
- [Django]-Why does DEBUG=False setting make my django Static Files Access fail?
- [Django]-Django Rest Framework: turn on pagination on a ViewSet (like ModelViewSet pagination)