Skip to main content

Wagtail model chooser

A plugin for Wagtail that provides convenience methods for setting up chooser modals for arbitrary models.

Installing

Install using pip:

pip install wagtail-modelchooser

Then add it to your INSTALLED_APPS:

INSTALLED_APPS = [
    # ...
    'wagtailmodelchooser',
    # ...
]

It works with Wagtail 4.0 and upwards. For older versions of Wagtail check previous versions of the package.

Quick start

To enable the chooser for your model, you must register the model. For simple cases, decorate your model with @register_model_chooser:

from django.db import models

from wagtailmodelchooser import register_model_chooser


@register_model_chooser
class Author(models.Model):
    name = models.CharField(max_length=255)

    def __str__(self):
        # The ``str()`` of your model will be used in the chooser
        return self.name

You can then use either FieldPanel in an edit handler definition, or ModelChooserBlock in a StreamField definition:

from wagtail.wagtailcore.blocks import RichTextBlock
from wagtail.wagtailcore.fields import StreamField
from wagtail.wagtailcore.models import Page
from wagtail.wagtailadmin.edit_handlers import FieldPanel
from wagtailmodelchooser.blocks import ModelChooserBlock

class Book(Page):
    name = models.CharField(max_length=255)
    author = models.ForeignKey(Author)

    content_panels = [
        FieldPanel('name'),
        FieldPanel('author'),
    ]

class ContentPage(Page):
    body = StreamField([
        ('text', RichTextBlock()),
        ('author', ModelChooserBlock('books.Author')),
    ])

    content_panels = [
        StreamFieldPanel('body'),
    ]

Customisation options

If you want to customize the content or behaviour of the model chooser modal you have several options. These are illustrated through some examples below.

If you wanted to add an additional filter field to the popup, you might do that as follows:

from django.db import models

from wagtailmodelchooser import register_model_chooser, Chooser


class City(models.Model):
    name = models.CharField(max_length=255)
    capital = models.BooleanField()

    def __str__(self):
        # The ``str()`` of your model will be used in the chooser
        return self.name

@register_model_chooser
class CityChooser(Chooser):
    model = City
    modal_template = 'app_name/city_modal.html'
    modal_results_template = 'app_name/city_modal_results.html'

    def get_queryset(self, request):
        qs = super().get_queryset(request)
        if request.GET.get('capital'):
            qs = qs.filter(capital=request.GET.get('capital') == '0')

        return qs

Since wagtailmodelchooser is built largely on the ChooserViewSet functionality already found in Wagtail, if you wish to do deeper customisation it is recommended to use that feature directly.

Metadata

Release files for wagtail-modelchooser 4.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for wagtail-modelchooser 4.0.1
File Size Uploaded Provenance
wagtail-modelchooser-4.0.1.tar.gz 9.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for wagtail-modelchooser 4.0.1
File Interpreter ABI Platform
wagtail_modelchooser-4.0.1-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 22.1 kB

Release files / wagtail-modelchooser-4.0.1.tar.gz

Download URL wagtail-modelchooser-4.0.1.tar.gz
Size 9.9 kB
Tags Source
SHA-256 checksum
How to use checksums
ec1c9c7598cdc019683c7e93ed95b0acf1d32b9b24e8a564b8f373bf48a9ed36
BLAKE2b-256 checksum
How to use checksums
cb161609db0fb3d7aaac1693424f6aafa6af90dbcd6f49295aae93739faeeabc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.7

Release files / wagtail_modelchooser-4.0.1-py2.py3-none-any.whl

Download URL wagtail_modelchooser-4.0.1-py2.py3-none-any.whl
Size 12.1 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
b7ea0fb81cdd7367ed90dff043289e264502d97a9297b7fb69c148a0a81cee6a
BLAKE2b-256 checksum
How to use checksums
6f62bf5d063ae5bd3e9ddfca750da1e22c3f57779a34ace1e864c672b580ad33
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.7

Security for wagtail-modelchooser 4.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page