Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can i divide the models in different files in django

Currently all my models are in models.py. Ist becomming very messy. Can i have the separate file like base_models.py so that i put my main models there which i don't want to touch

Also same case for views and put in separate folder rather than develop a new app

like image 676
Mirage Avatar asked Dec 05 '12 07:12

Mirage


2 Answers

Yes, it's doable. It's not particularly pretty though:

make models a module, so your directory structure looks like this:

- models
|- __init__.py
|- some_model.py
|- some_other_model.py
|- ...

now, the magic lies in __init__.py and some little extras in the models. __init__.py:

from some_model import SomeModel
from some_other_model import SomeOtherModel

__all__ = [ 
    'SomeModel',
    'SomeOtherModel',
]

some_model.py:

class SomeModel(models.Model):
    class Meta(object):
        app_label = 'yourapplabel'
        db_table = 'yourapplabel_somemodel'
like image 98
tback Avatar answered Oct 14 '22 05:10

tback


Everything acjohnson55 said, plus the fact that you need to specify the app_label attribute in each model's Meta class.

A link to an actual example on github: https://github.com/stefanfoulis/django-filer/tree/develop/filer/models

like image 41
Ioan Alexandru Cucu Avatar answered Oct 14 '22 05:10

Ioan Alexandru Cucu