Murcielago
Murcielago

Reputation: 1005

Django: migrate subapp model.py file

My issue is the following. How to migrate a the models in models.py file that is not the automatically generated with django-admin startapp appname

I have made a simple directory that illustrates my issue:

example_app
├── app.py
├── models.py
├── tenants
│   └── tenant1
│       ├── models.py
│       └── tenant1_views.py
├── tests.py
└── views.py

so what I want is to migrate what is inside of tenant1.models.py

I have not found any clue on how to achieve this, does anyone know?

Upvotes: 1

Views: 301

Answers (1)

Abdul Aziz Barkat
Abdul Aziz Barkat

Reputation: 21807

Django will look into models for all apps defined in INSTALLED_APPS, an app may not be created by django-admin startapp or manage.py startapp but may be added to INSTALLED_APPS. Django will look for a subclass of AppConfig in your app and if it doesn't find one it will use base AppConfig, hence you may want to add a apps.py file to your subapp and add an AppConfig to configure it properly:

In tenant1 add an apps.py:

from django.apps import AppConfig


class Tenant1Config(AppConfig):
    default_auto_field = 'django.db.models.AutoField'
    name = 'tenants.tenant1'

Next add your subapp to INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    'tenants.tenant1',
    ...
]

Next you should add a migrations directory with an __init__.py file to your app so that it may be detected directly by the migrations system. After this simply run:

python manage.py makemigrations

# Alternatively, the below command will automatically make a migrations directory
python manage.py makemigrations tenant1

Upvotes: 3

Related Questions