Skip to content

Commit 7ea8798

Browse files
committed
create superuser and run migrations in dev
1 parent 949d432 commit 7ea8798

File tree

9 files changed

+47
-16
lines changed

9 files changed

+47
-16
lines changed

samples/django-postgres/app/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ COPY . /code/
2424
RUN python manage.py collectstatic --noinput
2525

2626
# Start server
27-
CMD python manage.py migrate && python manage.py createsuperuser && gunicorn crm_platform.wsgi:application --bind 0.0.0.0:8000
27+
CMD python manage.py migrate && python manage.py createsuperauto && gunicorn app.wsgi:application --bind 0.0.0.0:8000
File renamed without changes.

samples/django-postgres/app/crm_platform/asgi.py renamed to samples/django-postgres/app/app/asgi.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
ASGI config for crm_platform project.
2+
ASGI config for app project.
33
44
It exposes the ASGI callable as a module-level variable named ``application``.
55
@@ -11,6 +11,6 @@
1111

1212
from django.core.asgi import get_asgi_application
1313

14-
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'crm_platform.settings')
14+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app.settings')
1515

1616
application = get_asgi_application()

samples/django-postgres/app/crm_platform/settings.py renamed to samples/django-postgres/app/app/settings.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Django settings for crm_platform project.
2+
Django settings for app project.
33
44
Generated by 'django-admin startproject' using Django 5.0.4.
55
@@ -56,7 +56,7 @@
5656
'whitenoise.middleware.WhiteNoiseMiddleware',
5757
]
5858

59-
ROOT_URLCONF = 'crm_platform.urls'
59+
ROOT_URLCONF = 'app.urls'
6060

6161
TEMPLATES = [
6262
{
@@ -74,7 +74,7 @@
7474
},
7575
]
7676

77-
WSGI_APPLICATION = 'crm_platform.wsgi.application'
77+
WSGI_APPLICATION = 'app.wsgi.application'
7878

7979

8080
# Database

samples/django-postgres/app/crm_platform/urls.py renamed to samples/django-postgres/app/app/urls.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
URL configuration for crm_platform project.
2+
URL configuration for app project.
33
44
The `urlpatterns` list routes URLs to views. For more information please see:
55
https://docs.djangoproject.com/en/5.0/topics/http/urls/

samples/django-postgres/app/crm_platform/wsgi.py renamed to samples/django-postgres/app/app/wsgi.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
WSGI config for crm_platform project.
2+
WSGI config for app project.
33
44
It exposes the WSGI callable as a module-level variable named ``application``.
55
@@ -11,6 +11,6 @@
1111

1212
from django.core.wsgi import get_wsgi_application
1313

14-
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'crm_platform.settings')
14+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app.settings')
1515

1616
application = get_wsgi_application()

samples/django-postgres/app/manage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
def main():
88
"""Run administrative tasks."""
9-
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'crm_platform.settings')
9+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app.settings')
1010
try:
1111
from django.core.management import execute_from_command_line
1212
except ImportError as exc:
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from django.core.management.base import BaseCommand
2+
from django.contrib.auth import get_user_model
3+
import logging
4+
5+
class Command(BaseCommand):
6+
def handle(self, *args, **options):
7+
User = get_user_model()
8+
logger = logging.getLogger(__name__)
9+
if not User.objects.filter(username='admin').exists():
10+
User.objects.create_superuser('admin', '[email protected]', 'admin')
11+
logger.info("Superuser 'admin' created automatically")
12+
else:
13+
logger.info("Superuser 'admin' already exists")

samples/django-postgres/compose.dev.yaml

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,47 @@ services:
99
- POSTGRES_PASSWORD=password
1010

1111
runmigrations:
12-
extends:
13-
file: compose.yaml
14-
service: django
1512
restart: on-failure
13+
build: ./app
1614
environment:
17-
- DEBUG=False
15+
- DB_HOST=db
16+
- DEBUG=True
17+
- POSTGRES_USER=django
18+
- POSTGRES_DB=django
1819
- POSTGRES_PASSWORD=password
20+
volumes:
21+
- "./app:/code"
1922
command: python manage.py migrate
2023
depends_on:
2124
- db
2225

26+
createsuperuser:
27+
restart: on-failure
28+
build: ./app
29+
environment:
30+
- DB_HOST=db
31+
- DEBUG=True
32+
- POSTGRES_USER=django
33+
- POSTGRES_DB=django
34+
- POSTGRES_PASSWORD=password
35+
volumes:
36+
- "./app:/code"
37+
command: python manage.py createsuperauto
38+
depends_on:
39+
- db
40+
2341
django:
2442
extends:
2543
file: compose.yaml
2644
service: django
2745
environment:
28-
- DEBUG=False
46+
- DEBUG=True
2947
- POSTGRES_PASSWORD=password
3048
volumes:
3149
- "./app:/code"
3250
command: python manage.py runserver 0.0.0.0:8000
3351
depends_on:
34-
- runmigrations
52+
- createsuperuser
3553

3654
volumes:
3755
postgres_data:

0 commit comments

Comments
 (0)