Skip to main content

Model choosers for Wagtail admin

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wagtail-modelchooser-4.0.0.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

wagtail_modelchooser-4.0.0-py2.py3-none-any.whl (12.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file wagtail-modelchooser-4.0.0.tar.gz.

File metadata

  • Download URL: wagtail-modelchooser-4.0.0.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.7

File hashes

Hashes for wagtail-modelchooser-4.0.0.tar.gz
Algorithm Hash digest
SHA256 42ca9339bf91da0b77fb78fdb9df0f48898449a3fadefff7eed7bffe8ffc4043
MD5 b550b6afac4c9821c2475162ee56d86b
BLAKE2b-256 08a137e4522af9d82ed865db2717740ab8a4187d7ab12d00ecffeab494ddc9f7

See more details on using hashes here.

File details

Details for the file wagtail_modelchooser-4.0.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for wagtail_modelchooser-4.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 750ecf19701e36474ee35015fce35ae5ff70472926233b96dc5e5bd6facac187
MD5 829933c98a1fb680db953dce41493a7d
BLAKE2b-256 c05ca544d94d3336fd26b3256299fcbdc0c991f29f43a2608abd3b6bc559332a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page