Skip to content

Commit afd4535

Browse files
committed
feat(wagtail): add home app with HomePage model and auto-run makemigrations
- Add home app template (models.py, apps.py, template, empty migrations/) with HomePage(Page) subclass matching the wagtail start pattern - Register home app in WAGTAIL_INSTALLED_APPS so it's enabled by --wagtail - Update _setup_wagtail_initial_data() to create a HomePage instance instead of a generic Page, so the Wagtail admin shows a real page type - Add makemigrations step to run_project() before migrate when Wagtail is installed — populates MIGRATION_MODULES-redirected packages on first run, no-op on subsequent runs - Update wagtail-support.rst with Home App section and makemigrations note
1 parent f0b142f commit afd4535

8 files changed

Lines changed: 81 additions & 4 deletions

File tree

docs/design/wagtail-support.rst

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,25 @@ Wagtail ships with its own migration files that include SQL-style assumptions in
2323
# ... one entry per Wagtail app
2424
}
2525
26-
Run ``manage.py makemigrations`` after configuring this to generate MongoDB-compatible migration files for each app. Django will use these in-project migrations instead of the ones bundled with Wagtail.
26+
``dbx project run`` automatically runs ``manage.py makemigrations`` before ``migrate`` whenever Wagtail is installed. On first run this populates the empty in-project packages with MongoDB-compatible migration files; on subsequent runs it is a no-op.
2727

28+
Home App
29+
---------
30+
31+
**Decision: ship a ``home`` app with a ``HomePage(Page)`` model**
32+
33+
The template includes a ``home`` app (mirroring what ``wagtail start`` generates) so the site has a concrete starting point for building out page types:
34+
35+
.. code-block:: python
36+
37+
# home/models.py
38+
class HomePage(Page):
39+
body = RichTextField(blank=True)
40+
content_panels = Page.content_panels + [FieldPanel("body")]
41+
42+
``dbx project run`` creates the Wagtail root page and a ``HomePage`` instance programmatically (not via a data migration), then points the default ``Site`` at that home page. The Wagtail admin is immediately usable on first run.
43+
44+
To add more page types, subclass ``Page`` in a new app or in ``home/models.py`` and run ``manage.py makemigrations``.
2845

2946
Custom App Configurations
3047
--------------------------

src/dbx_python_cli/commands/project.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,11 +1089,12 @@ def _setup_wagtail_initial_data(
10891089
" from django.conf import settings as _s",
10901090
" from django.contrib.contenttypes.models import ContentType",
10911091
" from wagtail.models import Locale, Site",
1092+
f" from {proj.name}.home.models import HomePage",
10921093
" lang = (getattr(_s, 'LANGUAGE_CODE', 'en') or 'en').split('-')[0][:2]",
10931094
" locale, _ = Locale.objects.get_or_create(language_code=lang)",
10941095
" page_ct, _ = ContentType.objects.get_or_create(app_label='wagtailcore', model='page')",
10951096
" root = Page.add_root(title='Root', slug='root', content_type=page_ct, locale=locale)",
1096-
" home = root.add_child(instance=Page(title='Home', slug='home', content_type=page_ct, locale=locale))",
1097+
" home = root.add_child(instance=HomePage(title='Home', slug='home', locale=locale))",
10971098
" print('wagtail_root_created')",
10981099
" if not Site.objects.exists():",
10991100
" Site.objects.create(hostname='localhost', root_page=home, is_default_site=True)",
@@ -1386,11 +1387,33 @@ def run_project(
13861387
venv_bin = str(Path(python_path).parent)
13871388
env["PATH"] = f"{venv_bin}{os.pathsep}{env.get('PATH', '')}"
13881389

1389-
# Run migrations before starting server
1390-
typer.echo(f"🗄️ Running migrations for project '{proj.name}'")
13911390
migrate_env = setup_django_command_env(
13921391
proj, ctx, settings=settings, include_dyld_fallback=False
13931392
)
1393+
1394+
# Generate migrations for Wagtail apps (MIGRATION_MODULES redirects them to
1395+
# in-project packages; makemigrations populates those packages on first run
1396+
# and is a no-op on subsequent runs when nothing has changed).
1397+
has_wagtail = subprocess.run(
1398+
[python_path, "-c", "import wagtail"],
1399+
cwd=proj.project_path,
1400+
env=migrate_env,
1401+
check=False,
1402+
capture_output=True,
1403+
)
1404+
if has_wagtail.returncode == 0:
1405+
typer.echo("⚙️ Generating Wagtail migrations...")
1406+
subprocess.run(
1407+
[python_path, "-m", "django", "makemigrations"],
1408+
cwd=proj.project_path,
1409+
env=migrate_env,
1410+
check=False,
1411+
capture_output=not verbose,
1412+
text=True,
1413+
)
1414+
1415+
# Run migrations before starting server
1416+
typer.echo(f"🗄️ Running migrations for project '{proj.name}'")
13941417
result = subprocess.run(
13951418
[python_path, "-m", "django", "migrate"],
13961419
cwd=proj.project_path,

src/dbx_python_cli/templates/project_template/project_name/home/__init__.py

Whitespace-only changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from django.apps import AppConfig
2+
from django.conf import settings
3+
4+
5+
class HomeConfig(AppConfig):
6+
name = "{{ project_name }}.home"
7+
verbose_name = "Home"
8+
9+
@property
10+
def default_auto_field(self):
11+
return getattr(settings, "DEFAULT_AUTO_FIELD", "django.db.models.BigAutoField")

src/dbx_python_cli/templates/project_template/project_name/home/migrations/__init__.py

Whitespace-only changes.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from wagtail.fields import RichTextField
2+
from wagtail.models import Page
3+
from wagtail.admin.panels import FieldPanel
4+
5+
6+
class HomePage(Page):
7+
body = RichTextField(blank=True)
8+
9+
content_panels = Page.content_panels + [
10+
FieldPanel("body"),
11+
]
12+
13+
class Meta:
14+
verbose_name = "Home page"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{% extends "base.html" %}
2+
{% load wagtailcore_tags %}
3+
4+
{% block content %}
5+
<div class="home-page">
6+
<h1>{{ page.title }}</h1>
7+
{% if page.body %}
8+
<div class="body">{{ page.body|richtext }}</div>
9+
{% endif %}
10+
</div>
11+
{% endblock %}

src/dbx_python_cli/templates/project_template/project_name/settings/wagtail.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"wagtail.sites",
2525
"modelcluster",
2626
"{{ project_name }}.settings.apps.wagtail.CustomTaggitConfig",
27+
"{{ project_name }}.home",
2728
]
2829

2930
WAGTAIL_MIDDLEWARE = [

0 commit comments

Comments
 (0)