Skip to content

Latest commit

 

History

History
65 lines (48 loc) · 1.82 KB

using_other_models.rst

File metadata and controls

65 lines (48 loc) · 1.82 KB

Using Other Models

:class:`AutocompletePanel` works with models other than :class:`~wagtail: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 :class:`~wagtail: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 :class:`~wagtail:wagtail.wagtailsnippets.edit_handlers.SnippetChooserPanel` usage with :class:`AutocompletePanel`.

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

Note

:doc:`wagtail-autocomplete <index>` 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 :class:`~wagtail:wagtail.wagtailsnippets.edit_handlers.SnippetChooserPanel` does. Instead, page_type should be used.