I have started work on a local app for myself that runs through the browser. Having recently gone through the django tutorial I'm thinking that it might be better to use django rather than just plain python.
There's one problem: I have at least 20 models and each will have many functions. Quite simply it's going to create one huge models file and probably huge views too. How do I split them up?
The models are all related so I can't simply make them into separate apps can I?
Django comes with six built-in apps that we can examine.
Django web applications access and manage data through Python objects referred to as models. Models define the structure of stored data, including the field types and possibly also their maximum size, default values, selection list options, help text for documentation, label text for forms, etc.
Ok what you should first know is that a Django project is composed of at least one app.
This is a pretty common need... I can't imagine wading through a models.py file that's 10,000 lines long :-)
You can split up the models.py
file (and views.py too) into a pacakge. In this case, your project tree will look like:
/my_proj /myapp /models __init__.py person.py
The __init__.py
file makes the folder into a package. The only gotcha is to be sure to define an inner Meta
class for your models that indicate the app_label for the model, otherwise Django will have trouble building your schema:
class Person(models.Model): name = models.CharField(max_length=128) class Meta: app_label = 'myapp'
Once that's done, import the model in your __init__.py
file so that Django and sync db will find it:
from person import Person
This way you can still do from myapp.models import Person
"I have at least 20 models" -- this is probably more than one Django "app" and is more like a Django "project" with several small "apps"
I like to partition things around topics or subject areas that have a few (1 to 5) models. This becomes a Django "app" -- and is the useful unit of reusability.
The overall "project" is a collection of apps that presents the integrated thing built up of separate pieces.
This also helps for project management since each "app" can become a sprint with a release at th end.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With