Using Other Models

AutocompletePanel works with models other than wagtail.wagtailcore.Page and subclasses of it.

Selecting Snippets

For example, we have a Django model Link that we have registered as a snippet. We also have a BlogPage model that would traditionally use a wagtail.wagtailsnippets.edit_handlers.SnippetChooserPanel

from django.db import models

from wagtail.wagtailadmin.edit_handlers import FieldPanel
from wagtail.wagtailcore.models import Page
from wagtail.wagtailsnippets.edit_handlers import SnippetChooserPanel
from wagtail.wagtailsnippets.models import register_snippet

@register_snippet
class Link(models.Model):
    title = models.CharField(max_length=255)
    url = models.URLField()

    panels = [
        FieldPanel('title'),
        FieldPanel('url'),
    ]


class BlogPage(Page):
    external_link = models.ForeignKey(
        'app_label.Link',
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
    )

    content_panels = [
        SnippetChooserPanel('external_link'),
    ]

We can replace the wagtail.wagtailsnippets.edit_handlers.SnippetChooserPanel usage with AutocompletePanel.

panels = [
    AutocompletePanel('external_link', page_type='app_label.Link'),
]

Note

wagtail-autocomplete does not offer two separate edit handlers like Wagtail does for Page and Snippet. As such, AutocompletePanel does not support the snippet_type kwarg that wagtail.wagtailsnippets.edit_handlers.SnippetChooserPanel does. Instead, page_type should be used.