1

I have a project with several apps and many data models. I'm using Django 1.7 and Python 2.7.

I've organized the models into app-level modules.

- common/
  -- models/
    --- __init__.py
    --- these_models.py
    --- those_models.py

I've added a new file in this structure and Django's makemigration command is not detecting changes.

If I put the new models in an existing model file the migration files are created perfectly, everything migrates and runs great. Once I put them into a new file Django doesn't find them. They aren't in a new app - it's an existing app/models/ module, just a new file. I don't import * (ewwww) in the __init__.py or anything.

In Django 1.4 I had to use the Meta's app_label but don't do this anymore.

Any thoughts? Will I need to make the migration files manually (I have no problem doing this)?

Rico
  • 5,692
  • 8
  • 46
  • 63
  • http://stackoverflow.com/questions/5534206/how-do-i-separate-my-models-out-in-django – Othman May 04 '15 at 22:44
  • This is not relevant to my question as I'm using Django 1.7. Also, I mentioned in my question that is not relevant. Did you read my question? – Rico May 04 '15 at 22:44
  • You should import your models in the `__init__.py` inside `models`. No one is telling you to use `*`. – snahor May 05 '15 at 00:18
  • @snahor This does not follow our current project paradigm. Django can handle what I'm describing and I'm not sure why it's not this time. – Rico May 06 '15 at 15:46

1 Answers1

6

Django does now support models in subfolders without needing to specify the Meta class and app_label but it's still python and doesn't magically load all modules in the models folder.

You still need to import your models into your app/models/__init__.py.

Yuji 'Tomita' Tomita
  • 115,817
  • 29
  • 282
  • 245
  • This is not true. My project has about 8 model modules with about 2 dozen model files across the modules. Not one is imported in the `app/models/__init__.py`. We just reference all our models as `from app.models.these_models import SomeModel`. Django seems to handle this paradigm just fine. (other than this use case) – Rico May 06 '15 at 15:44
  • 2
    @Rico in that case you are "getting lucky" by the fact there are imports for those models. If there is a period of time a model in one of those files is not imported by a module auto-imported by django, `makemigrations` would fail to see the models and delete it. Sounds a little dangerous! This is also the problem with why your new models aren't picked up by makemigrations. Nothing is importing them. This is a dependency nobody would expect (removing an import should not cause massive changes in the way the app works) and I recommend importing in `__init__.py` where django expects to find them – Yuji 'Tomita' Tomita May 06 '15 at 17:51