Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refactor large models.py file in Django app

After reading monokrome's answer to Where should Django manager code live?, I've decided to split a large models.py into smaller, more manageable files. I'm using the folder structure

foodapp/
    models/
        __init__.py #contains pizza model
        morefood.py #contains hamburger & hotdog models

In __init__.py, I import the models from morefood.py with

from morefood import hamburger, hotdog

However, when I run python manage.py syncdb, the only table created is foodapp_pizza - What do I need to do to get Django to create tables for the models I have imported from morefood.py?

like image 918
Alasdair Avatar asked Dec 11 '09 00:12

Alasdair


2 Answers

Try this:

foodapp/
    __init__.py
    models.py
    /morefood
        __init__.py
        hamburger.py
        hotdog.py

and do this in models.py:

from foodapp.morefood.hamburger import *
from foodapp.morefood.hotdog import *

as suggested in this blogpost.

like image 143
rinti Avatar answered Oct 24 '22 06:10

rinti


Or, in your morefood.py models, add the following to the Meta:

class Meta:
    app_label = 'foodapp'

Then syncdb will work with your existing structure

like image 24
kibitzer Avatar answered Oct 24 '22 08:10

kibitzer