About 20 models in 1 django 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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)