views:

1147

answers:

7

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 cant's simply make them into separate apps can I?

A: 

You can break up the models over multiple files. This goes for views as well.

Albinofrenchy
A: 

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.

Oli
As Jarret Hardie said, creating a python package (i.e. a directory with an __init__.py file in it) called models would be the best way to do this.
Flávio Amieiro
+10  A: 

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

Jarret Hardie
It's theoretically working but running the line from the tutorial "python manage.py sql gui" doesn't do anything, any idea what's up?
Teifion
That usually means that django can't find the models. Make sure you've got the imports in your models.__init__.py file, and that you've set the app_label for each model. Also make sure you've delete the old models.py file.
Jarret Hardie
This is technically correct, but _almost_ downvote-worthy as it's such a bad idea in practice. If you have enough models to think about splitting your models.py, you should split into multiple apps.
Carl Meyer
Carl, I agree that splitting into multiple apps is valuable in larger projects. S. Lott's points in his post express the benefits of this quite well.
Jarret Hardie
+9  A: 

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.

NathanD
+1. There's no law against apps depending on each other.
muhuk
+12  A: 

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

S.Lott
But if the models have to relate to each other how can I place them in separate apps?
Teifion
One model can easily reference models in another package. You almost always have "higher-level" and "lower-level" models. Implement the lower-level models in one app. The higher-level apps can depend on the lower-level app's models.
S.Lott
Any time you set up a foreign key to the User model in django.contrib.auth, you're relating across separate apps...
James Bennett
A: 

Great post - both the instructions on how to split up a model file into smaller parts and the discussion/debate regarding when to use multiple apps.

I found I had to move my fixture data into the models package so it would be reloaded when running syncdb. Just move the "fixtures" folder into the new "models" folder and it should work. Just thought I would share that detail in case its helpful to someone else like me who is relatively new to django.

Glenn Snyder
A: 

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

Flávio Amieiro