wagtailwagtail-adminwagtail-snippet

Can I create a custom Column of type "boolean" in SnippetViewSet


I have a SnippetViewSet in Wagtail 5 which is in fact a boolean. The field is added in the queryset using an annotation:

class PartnerViewSet(SnippetViewSet):
    model = Partner

    list_display = [
        Column("is_current_partner", label=_("Current partner"), accessor="is_current_partner")
    ]

    def get_queryset(self, request):
        return (
            Partner.objects.all()
            # To check whether the partner is a current partner or not
            .annotate(
                is_current_partner=Exists(
                    EditionPartnership.objects.filter(
                        edition__is_current=True,
                        partner_id=OuterRef("pk"),
                    )
                )
            )
        )

However in Wagtail admin, this displays as either "True" or as an empty value if False. Ideally this would be a checkbox or something similar to Django Admin.

Is this possible out of the box?


Solution

  • Based on the response from cnk, I ended up with this by overriding the get_value() method:

    class BooleanColumn(Column):
        def get_value(self, instance):
            value = super().get_value(instance)
    
            return "✅" if value else "⛔"
    

    That way no changes to the template etc are needed.