Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

About 20 models in 1 django app

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?

like image 837
Teifion Avatar asked May 13 '09 17:05

Teifion


People also ask

How many apps should a Django project have?

Django comes with six built-in apps that we can examine.

What are models in Django used for?

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.

What is the minimum number of apps required for Django?

Ok what you should first know is that a Django project is composed of at least one app.


2 Answers

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

like image 65
Jarret Hardie Avatar answered Sep 27 '22 21:09

Jarret Hardie


"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.

like image 24
S.Lott Avatar answered Sep 27 '22 20:09

S.Lott