Model choosers for Wagtail admin
Reason this release was yanked:
bad
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for wagtail-modelchooser-3.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4d17deddd0e706cbd267e431e9321ee8ab3a8f37cc1f1cb05cbcab3aa1b11350 |
|
MD5 | e6db5b79cf96f05b540237487341bf87 |
|
BLAKE2b-256 | a69e2c073667246569ef1b362e07de7b9b765de0b715b7e66ed7d5fc5891a36b |
Hashes for wagtail_modelchooser-3.0.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a2142472e7d46d7ff7ab73e143e584ed4f03f1d8c989cc334a91afa8367d96a5 |
|
MD5 | 98d4fd7e0d34eb95fa9d665cd03ca9ff |
|
BLAKE2b-256 | 8e4fee6349722fb44b3a3429cc5af4c3dac9c37df30808fadcecc5a3b7e9ce04 |