Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wagtail.contrib.forms.models.AbstractForm subclasses without a template attribute cannot be instantiated in migrations #11911

Open
jams2 opened this issue Apr 30, 2024 · 0 comments
Labels
status:Unconfirmed Issue, usually a bug, that has not yet been validated as a confirmed problem. type:Bug

Comments

@jams2
Copy link
Contributor

jams2 commented Apr 30, 2024

Issue Summary

wagtail.contrib.forms.models.AbstractForm subclasses without a template attribute cannot be instantiated in migrations.

gitpod /workspace/wagtail-gitpod (main) $ ./manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, home, sessions, taggit, wagtailadmin, wagtailcore, wagtaildocs, wagtailembeds, wagtailforms, wagtailimages, wagtailredirects, wagtailsearch, wagtailusers
Running migrations:
  Applying home.0007_migrate_form_page...Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/workspace/.pip-modules/lib/python3.8/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/workspace/.pip-modules/lib/python3.8/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/workspace/.pip-modules/lib/python3.8/site-packages/django/core/management/base.py", line 412, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/workspace/.pip-modules/lib/python3.8/site-packages/django/core/management/base.py", line 458, in execute
    output = self.handle(*args, **options)
  File "/workspace/.pip-modules/lib/python3.8/site-packages/django/core/management/base.py", line 106, in wrapper
    res = handle_func(*args, **kwargs)
  File "/workspace/.pip-modules/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 356, in handle
    post_migrate_state = executor.migrate(
  File "/workspace/.pip-modules/lib/python3.8/site-packages/django/db/migrations/executor.py", line 135, in migrate
    state = self._migrate_all_forwards(
  File "/workspace/.pip-modules/lib/python3.8/site-packages/django/db/migrations/executor.py", line 167, in _migrate_all_forwards
    state = self.apply_migration(
  File "/workspace/.pip-modules/lib/python3.8/site-packages/django/db/migrations/executor.py", line 252, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/workspace/.pip-modules/lib/python3.8/site-packages/django/db/migrations/migration.py", line 132, in apply
    operation.database_forwards(
  File "/workspace/.pip-modules/lib/python3.8/site-packages/django/db/migrations/operations/special.py", line 193, in database_forwards
    self.code(from_state.apps, schema_editor)
  File "/workspace/wagtail-gitpod/home/migrations/0007_migrate_form_page.py", line 8, in migrate_form_page
    for page in FormPage.objects.all():
  File "/workspace/.pip-modules/lib/python3.8/site-packages/django/db/models/query.py", line 398, in __iter__
    self._fetch_all()
  File "/workspace/.pip-modules/lib/python3.8/site-packages/django/db/models/query.py", line 1881, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/workspace/.pip-modules/lib/python3.8/site-packages/django/db/models/query.py", line 122, in __iter__
    obj = model_cls.from_db(
  File "/workspace/.pip-modules/lib/python3.8/site-packages/django/db/models/base.py", line 582, in from_db
    new = cls(*values)
  File "/workspace/.pip-modules/lib/python3.8/site-packages/wagtail/contrib/forms/models.py", line 178, in __init__
    name, ext = os.path.splitext(self.template)
AttributeError: 'FormPage' object has no attribute 'template'

There are scenarios where users might not explicitly declare template or landing_page_template attributes:

  • they are happy with the default naming scheme
  • they are using Wagtail in a headless architecture

Steps to Reproduce

  1. Start a new project with wagtail start myproject
  2. Edit models.py as follows
from django.db import models

from wagtail.models import Page
from wagtail.contrib.forms.models import AbstractForm


class HomePage(Page):
    pass

class FormPage(AbstractForm):
    pass
  1. ./manage.py makemigrations
  2. ./manage.py migrate
  3. Sign in to admin and create an instance of FormPage.
  4. ./manage.py makemigrations --empty -n migrate_form_page home
  5. Edit the new, empty migration:
# Generated by Django 4.2.11 on 2024-04-30 16:33

from django.db import migrations


def migrate_form_page(apps, _):
    FormPage = apps.get_model("home", "FormPage")
    for page in FormPage.objects.all():
        continue


class Migration(migrations.Migration):
    dependencies = [
        ("home", "0006_formpage"),
    ]

    operations = [
        migrations.RunPython(migrate_form_page, migrations.RunPython.noop),
    ]
  1. ./manage.py migrate

This will cause the error.

  • I have confirmed that this issue can be reproduced as described on a fresh Wagtail project: yes

Technical details

  • Python version: 3.8.12
  • Django version: 4.2.11
  • Wagtail version: 6.0.2
  • Browser version: N/A

Working on this

I am happy to submit a patch for this issue

@jams2 jams2 added status:Unconfirmed Issue, usually a bug, that has not yet been validated as a confirmed problem. type:Bug labels Apr 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:Unconfirmed Issue, usually a bug, that has not yet been validated as a confirmed problem. type:Bug
Projects
None yet
Development

No branches or pull requests

1 participant