31
“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.
76
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
- [Django]-How do I convert datetime.timedelta to minutes, hours in Python?
- [Django]-Plug in django-allauth as endpoint in django-rest-framework
- [Django]–bash: ./manage.py: Permission denied
16
The models are all related so I cant’s
simply make them into separate apps
can I?
You can separate them into separate apps. To use a model in one app from another app you just import it in the same way you would import django.contrib apps.
- [Django]-Django Rest Framework with ChoiceField
- [Django]-How to execute a GROUP BY … COUNT or SUM in Django ORM?
- [Django]-Django type object Http404 has no attribute get
5
Having 20 models in one app might be a sign that you should break it up in smaller ones.
The purpose of a Django app is to have a small single-purpose piece of code, that fits nicelly together.
So, if you had a e-commerce site, you might have a shopping_cart app, a billing app, and so on.
Keep in mind that there is really no problem in apps depending on each other (although it’s always better if they can be decoupled), but you should not have an app doing two very distinct things.
The article Django tips: laying out an application might help you. As always, take everything you read with a grain of salt (including this answer).
- [Django]-Django template and the locals trick
- [Django]-How can I subtract or add 100 years to a datetime field in the database in Django?
- [Django]-How to format time in django-rest-framework's serializer?
- [Django]-Django: How to manage development and production settings?
- [Django]-Django dynamically filtering with q objects
- [Django]-Manage.py runserver
0
You can split them into separate files and simply have imports at the top of your main models.py field.
Whether you’d really want to is another question.
- [Django]-Django.contrib.auth.logout in Django
- [Django]-Django signals vs. overriding save method
- [Django]-What is the difference between postgres and postgresql_psycopg2 as a database engine for django?