8

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?

Community
  • 1
  • 1
Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • 1
    Everyone has different ideas of how large a file is "too large," but in general I find that if an app requires refactoring of its models.py, that generally indicates an app that isn't doing "just one thing" and it's the app itself that needs to be refactored into multiple apps. Just a thought. – Carl Meyer Dec 11 '09 at 17:29
  • Both answers below work well. I prefer kibitzer's slightly belows it I can keep all the models in the models folder. I'll keep an eye on the ticket (http://code.djangoproject.com/ticket/6961) mentioned in the blog post that Baresi mentions. – Alasdair Dec 11 '09 at 22:11
  • @Carl – good point. In this particular case, I have ended up splitting the app into two. – Alasdair Dec 11 '09 at 22:12

2 Answers2

7

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.

Tony
  • 9,672
  • 3
  • 47
  • 75
rinti
  • 1,273
  • 1
  • 12
  • 16
  • 1
    Blog posting is now 404, but this article seems to be the same content: http://www.justinlilly.com/this-and-that/fitures_model_subdir.html – scwagner Apr 08 '12 at 02:51
3

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

kibitzer
  • 4,479
  • 1
  • 21
  • 20