Wagtail CMS: How to get a searchable ForeignKey field in Page editor (instead of huge dropdown)?

I’m using Wagtail 7.1.2 and have a Page model with a ForeignKey to a model that contains a very large number of rows (cities).

Example:

class CityPage(Page):
    cityobject = models.ForeignKey(
        CityTranslated,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
    )

CityTranslated is a proxy model:

@register_snippet
class CityTranslated(City):
    class Meta:
        proxy = True
        ordering = ("name",)

    def __str__(self):
        return f"{self.name} – Population: {self.population}"

Problem

In the Wagtail page editor, this ForeignKey is rendered as a dropdown, which is unusable because there are tens of thousands of cities.

What I want is:

  • a search / autocomplete field

  • similar to Django admin’s autocomplete_fields

  • usable inside the Wagtail page editor

  • preferably without converting the model to a full snippet or changing the DB schema

What I tried

  • Using Wagtail snippets → search only works if the model is not a proxy

  • Custom ChooserViewSet → modal opens, but no search field in Wagtail 7

  • Django admin autocomplete_fields → works in admin, but not in Wagtail CMS

  • Looked into Wagtail chooser APIs, but custom choosers don’t seem to support search for arbitrary models in Wagtail 7

Question

What is the recommended way in Wagtail 7 to make a ForeignKey field searchable/autocomplete in the page editor?

  • Is this possible with core Wagtail?

  • Is a snippet wrapper the only supported solution?

  • Or is a third-party package (e.g. wagtail-autocomplete) the intended approach?

I’d appreciate guidance on the best practice here.

Вернуться на верх