Skip to main content

A widget for Wagtail's admin that allows you to create and select related items

Project description

wagtail-instance-selector

A widget for Wagtail's admin that allows you to create and select related items.

Features and screenshots

Customizable widget display

By default, widgets appear similar to other Wagtail elements, but they can be customised to include images and other items.

Item selection reuses the admin's list views to ensure consistent UIs with filtering.

Inline creation

Items can be created within the selection widget.

After creation, items can be selected from the success message or from the list view.

Installation

pip install wagtail-instance-selector

and add 'instance_selector' and 'wagtail_modeladmin' to INSTALLED_APPS.

If you're using Django 3+, you will need to change Django's iframe security flag in your settings:

X_FRAME_OPTIONS = 'SAMEORIGIN'

Documentation

Using the widget as a field panel

from django.db import models
from instance_selector.edit_handlers import InstanceSelectorPanel


class Shop(models.Model):
    pass


class Product(models.Model):
    shop = models.ForeignKey(Shop, on_delete=models.CASCADE)

    panels = [InstanceSelectorPanel("shop")]

Using the widget in a stream field

from django.db import models
from wagtail.admin.panels import FieldPanel
from wagtail.fields import StreamField
from instance_selector.blocks import InstanceSelectorBlock


class Product(models.Model):
    pass


class Service(models.Model):
    pass


class Shop(models.Model):
    content = StreamField([
        ("products", InstanceSelectorBlock(target_model="test_app.Product")),
        ("services", InstanceSelectorBlock(target_model="test_app.Service")),
    ], use_json_field=True)

    panels = [FieldPanel("content")]

To create reusable blocks, you can subclass InstanceSelectorBlock.

from instance_selector.blocks import InstanceSelectorBlock


class ProductBlock(InstanceSelectorBlock):
    def __init__(self, *args, **kwargs):
        target_model = kwargs.pop("target_model", "my_app.Product")
        super().__init__(target_model=target_model, **kwargs)

    class Meta:
        icon = "image"

# ...

StreamField([
    ("products", ProductBlock()),
])

Customizing the widget's display and behaviour

from wagtail_modeladmin.options import ModelAdmin, modeladmin_register
from instance_selector.registry import registry
from instance_selector.selectors import ModelAdminInstanceSelector
from .models import MyModel


@modeladmin_register
class MyModelAdmin(ModelAdmin):
    model = MyModel


class MyModelInstanceSelector(ModelAdminInstanceSelector):
    model_admin = MyModelAdmin()

    def get_instance_display_title(self, instance):
        if instance:
            return "some title"

    def get_instance_display_image_url(self, instance):
        if instance:
            return "/url/to/some/image.jpg"

    def get_instance_display_image_styles(self, instance):
        # The `style` properties set on the <img> element, primarily of use
        # to work within style+layout patterns
        if instance:
            return {
                'max-width': '165px',
                # ...
            }

    def get_instance_display_markup(self, instance):
        # Overriding this method allows you to completely control how the
        # widget will display the relation to this specific model
        return "<div> ... </div>"

    def get_instance_display_template(self):
        # The template used by `get_instance_display_markup`
        return "instance_selector/instance_selector_widget_display.html"

    def get_instance_selector_url(self):
        # The url that the widget will render within a modal. By default, this
        # is the ModelAdmin"s list view
        return "/url/to/some/view/"

    def get_instance_edit_url(self, instance):
        # The url that the user can edit the instance on. By default, this is
        # the ModelAdmin"s edit view
        if instance:
            return "/url/to/some/view/"


registry.register_instance_selector(MyModel, MyModelInstanceSelector())

Note that the ModelAdminInstanceSelector is designed for the common case. If your needs are more specific, you may find some use in instance_selector.selectors.BaseInstanceSelector.

Rationale & Credits

Largely, this is a rewrite of neon-jungle/wagtailmodelchooser that focuses on reusing the functionality in the ModelAdmins. We had started a large build using wagtailmodelchooser heavily, but quickly ran into UI problems when users needed to filter the objects or create them inline. After neon-jungle/wagtailmodelchooser#11 received little response, the decision was made to piece together parts from the ecosystem and replicate the flexibility of django's raw_id_fields, while preserving the polish in Wagtail's UI.

Much of this library was built atop of the work of others, specifically:

Development notes

Upgrading Wagtail versions

When upgrading this, ensure both InstanceSelectorPanel and InstanceSelectorBlock are tested manually via the example projects as they use different JavaScript integration approaches.

The example project is setup for testing via:

  • Shop model admin for InstanceSelectorBlock testing.
  • Product model admin InstanceSelectorPanel testing.

Run tests

pip install -r requirements.txt
python runtests.py

Linting and formatting

pip install -r requirements.txt
ruff check
ruff format

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_instance_selector-3.1.1.tar.gz (22.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wagtail_instance_selector-3.1.1-py3-none-any.whl (21.9 kB view details)

Uploaded Python 3

File details

Details for the file wagtail_instance_selector-3.1.1.tar.gz.

File metadata

File hashes

Hashes for wagtail_instance_selector-3.1.1.tar.gz
Algorithm Hash digest
SHA256 851918e9c496d0de15c7a2a407792779c260be16d56181c7da34e4919bbfe405
MD5 df3e4df3a33acffe7f041f702e10f63c
BLAKE2b-256 7e477ff98fd5709a5b153c79e94e44bb96e15550630f0a36716a374957d0fff6

See more details on using hashes here.

File details

Details for the file wagtail_instance_selector-3.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for wagtail_instance_selector-3.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fe5f2a2d2a7b874e9ae81ad6cb529d572f9779be328cc6b6fcce630c28dd4ce8
MD5 dde9a921504fec39667a289f70613c63
BLAKE2b-256 e7e272beb97bec8ae1591c728338966d7e293f453ed29e91e6dae3f7f7c01850

See more details on using hashes here.

Supported by

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