Django docs suggest this method to organize models in a package/app by splitting models.py into several files. However, it's worth noting that:
- This method works only when at max 1 model is defined per file;
- In the app/models/__init__.py, you'll need to import every individual model class from every file created inside app/models/.
Say, you organized models like this:
Then, your PlanningDteApp/models/__init__.py would look like this: - You'd still be able to import all models in app/admin.py by doing from .models import *
- You'd still be able to import all models of an app (App1) into another app (App2) by doing:
from App1.models import *
[Learnings:] In Django, file & folder names essentially act as namespaces/paths in their own right and you'd need to use the entire namespace/path to get to the desired class.
Useful references:
- https://docs.djangoproject.com/en/4.0/topics/db/models/#organizing-models-in-a-package
- https://stackoverflow.com/questions/6336664/split-models-py-into-several-files
Comments
Post a Comment