physicalattraction
physicalattraction

Reputation: 6858

Django migrations in subfolders

I have the following project structure:

bm_app_1
 | contents here
bm_app_2
 | contents here
bm_common
 | __init__.py
 | deletable
 |  | __init__.py
 |  | behaviors.py
 |  | models.py
 | timestampable
 |  | __init__.py
 |  | behaviors.py

The files in app bm_common define managed models, that I want in migration files. When I run python managepy makemigrations however, the files inside the subfolders of the app bm_common are not taken into account. All apps are in INSTALLED_APPS

PREREQ_APPS = [
    required apps here
]
PROJECT_APPS = [
    'bm_common',
    'bm_app_1',
    'bm_app_2'
]
INSTALLED_APPS = PREREQ_APPS + PROJECT_APPS

Is there a way to change the behavior of makemigrations to look into subfolders as well? If not, what would be a good suggestion to make this split? I do not want to have all behaviors in one behaviors.py, because it grows too big and was causing circular references for me.

Upvotes: 2

Views: 2227

Answers (2)

TheDninoo
TheDninoo

Reputation: 1

You just need to import them in the init.py module, and it'will work !

Upvotes: 0

knbk
knbk

Reputation: 53679

The models need to be imported in some way when the app registry is populating all models, otherwise they are not registered and Django doesn't know about them. The easiest solution is to create a bm_common/models.py file and import all models in there:

from .deletable.models import ModelA, ModelB
from .timestampable.models import ModelC
...

Upvotes: 2

Related Questions