Skip to main content

Model choosers for Wagtail admin

Project description

A plugin for Wagtail that provides a ModelChooserPanel and ModelChooserBlock 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 2.2 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 ModelChooserPanel 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, StreamFieldPanel
from wagtailmodelchooser.blocks import ModelChooserBlock
from wagtailmodelchooser.edit_handlers import ModelChooserPanel

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

    content_panels = [
        FieldPanel('name'),
        ModelChooserPanel('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 = 'wagtailmodelchooser/city_modal.html'
    modal_results_template = \
        'wagtailmodelchooser/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
{% extends 'wagtailmodelchooser/modal.html' %}

{% block search_fields %}
<input type="search" name="q" id="id_q" placeholder="Search..." autocomplete="off">
<input type="checkbox" name="capital">
{% endblock %}
{% extends 'wagtailmodelchooser/results.html' %}

{% block extra_table_headers %}
<th>Is Capital</th>
{% endblock %}

{% block extra_table_row_columns %}
<td>{{instance.capital}}</td>
{% endblock %}

You can also register hooks to modify the javascript behaviour of the model. See the add*Hook methods on window.wagtail.ui.ModelChooser.

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-2.3.1.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

wagtail_modelchooser-2.3.1-py2.py3-none-any.whl (13.4 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: wagtail-modelchooser-2.3.1.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/33.1.1 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.5.3

File hashes

Hashes for wagtail-modelchooser-2.3.1.tar.gz
Algorithm Hash digest
SHA256 4a69d1807fd97482575ef99892ff9d8c811100413ee86207de014f0cd41729c4
MD5 e4c93e5736bd625de4b7cc5d35d1bec1
BLAKE2b-256 f8f030fe88045fb40c81bffe3d1867d943b755ba8717e98fc95babb3f734725f

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: wagtail_modelchooser-2.3.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 13.4 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/33.1.1 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.5.3

File hashes

Hashes for wagtail_modelchooser-2.3.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 dc55d880e755efae12d18a51ca9aabde32b9e0476a7a878eaa0b524b14a52f20
MD5 9664f1a96e72c6dd5176c7f8b715325e
BLAKE2b-256 5788facf5c845c65e52d56e00cea578b9ccc09ae33f1a531781510141b43b971

See more details on using hashes here.

Provenance

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