24
loading...
This website collects cookies to deliver better user experience
models.py
, but since you're implementing this third party library outside of the context of your monolith, there is no manage.py
to be found. But you want to run your migrations!migrations
inside your django package. Once you successfully run the migrations, they will be placed here.scripts
folder on the root of the project.
makemigrations.py
file. Put this content inside it. With that you'll be able to run the makemigrations script:
import os
import sys
if __name__ == "__main__":
os.environ.setdefault(
"DJANGO_SETTINGS_MODULE", "scripts.mock_settings"
)
from django.core.management import execute_from_command_line
args = sys.argv + ["makemigrations"]
execute_from_command_line(args)
mock_settings.py
too. This file is necessary for the makemigrations
command since any Django execution (and makemigrations is) requires a settings file. However, since we only need it to generate the migrations we can have a sample and dumb settings file just to do this operation. Put this content in the file:
import os
SECRET_KEY = 'Not a secret key at all, actually'
DEBUG = True
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": "postgres",
"USER": "postgres",
"PASSWORD": "example",
"HOST": "localhost",
"PORT": "60000",
}
}
INSTALLED_APPS = [
# Django apps necessary to run the admin site
'django.contrib.auth',
'django.contrib.admin',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.staticfiles',
'django.contrib.messages',
# And this app
'your_django_lib',
]
STATIC_URL = '/static/'
MIDDLEWARE = [
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'OPTIONS': {
'context_processors': [
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
}
},
]
DATABASE
settings and configure a sqlite database. However sqlite does not support the likes of the JSONField, and migrations with JSONFields won't work.version: '3.6'
services:
postgres:
image: postgres:9.5-alpine
volumes:
- postgres_data:/var/lib/postgresql/data
ports:
- 60000:5432
expose:
- "5432"
restart: always
environment:
POSTGRES_PASSWORD: example
volumes:
postgres_data:
your-django-lib/
├─ your_django_lib/
│ ├─ migrations/
│ │ ├─ __init__.py
│ ├─ __init__.py
│ ├─ models.py
├─ scripts/
│ ├─ __init__.py
│ ├─ mock_settings.py
│ ├─ makemigrations.py
├─ .gitignore
├─ docker-compose.yml
makemigrations
script. Depending on your virtual environment you'll have to execute the command in a different manner. With poetry for example you'd run it like this:poetry run python scripts/makemigrations.py