Custom Wagtail chooser viewset chosen_view_class is ignored

149 Views Asked by At

I have a basic custom ChooserViewSet to add some functionality to returned chosen data and to specify the widget to use. The model associated with the viewset is a registered snippet.

I have specified the chosen_view_class in the viewset with a custom class specified in the same module.

My views.py is as follows:

from django.utils.functional import cached_property
from django.views.generic.base import View
from wagtail.admin.views.generic.chooser import ChosenResponseMixin, ChosenViewMixin
from wagtail.admin.viewsets.chooser import ChooserViewSet

from .models import Product
from .widgets import ProductChooser


class ProductChosenResponse(ChosenResponseMixin):
    def get_chosen_response(self, item):
        return self._wrap_chosen_response_data({
            "id": str(self.get_object_id(item)),
            "string": item.chosen_string,
            "edit_url": self.get_edit_item_url(item),
        })

class ProductChosenView(ChosenViewMixin, ProductChosenResponse, View):
    pass

class ProductChooserViewSet(ChooserViewSet):
    model = Product
    chosen_view_class = ProductChosenView

    @cached_property
    def widget_class(self):
        return ProductChooser(self.model, icon=self.icon)

product_chooser_viewset = ProductChooserViewSet("product_chooser")

with hook:

from wagtail import hooks
from .product.views import product_chooser_viewset

@hooks.register('register_admin_viewset')
def register_product_chooser_viewset():
    return product_chooser_viewset

If I set a breakpoint in ProductChosenResponse, the code never gets run when an item is chosen, instead it runs the default ChosenResponseMixin in the base generic view. If I set a breakpoint there, I see the calling class is a SnippetChosenView instead of my custom ProductChosenView.

type(self)
<class 'wagtail.snippets.views.chooser.SnippetChosenView'>

I can see ProductChooserViewSet is being loaded as the correct widget is being used (and not if I disable the widget_class definition).

I can create a ProductChooserViewSet instance at the command line and get the correct chosen view class:

In [14]: pcvs.chosen_view_class
Out[14]: product.views.ProductChosenView

Any help getting the correct chosen_view_class code to run would be greatly appreciated!

0

There are 0 best solutions below