Caspar Wylie
Caspar Wylie

Reputation: 49

Why are there one model.py per app instead of just one model.py out through the whole project?

I was just wondering why each project does not just have one model.py file, considering its just a file full of classes ( acting as database tables), because the whole project runs on one database, why can there be more than one models.py file if all files work with the same database?

Thanks.

Upvotes: 1

Views: 54

Answers (3)

OBu
OBu

Reputation: 5177

Ususally you will start writing one app. Once in a time, you will recognize that there are features which are not very tightly related (e.g. user management or different sub-parts). Additionally, your models.py will start to be lengthy and want to have a clearer structure. This is the point in time where you start splitting your project in independent sub-parts - the apps. Still, they will work with the same database. And even better: some friendly guys might have built apps whih you can include in your project - each bringing teir models and - of course - interacting with your database. If everything in yourproject is closely related, there is no need for different apps.

Upvotes: 0

JAL
JAL

Reputation: 21563

Django is set up to have projects that are collections of reusable, self contained apps. Each has its own model.py because they're tied closely to the views and templates for that app but may not be needed for the rest of the project.

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798626

So that the apps can be taken and used with a different database if desired without needing to modify the code (much).

Upvotes: 1

Related Questions