Register AdminSnippetChooser from SnippetChooserViewSet.on_register()

pull/9821/head
Sage Abdullah 2023-03-09 13:37:45 +00:00 zatwierdzone przez LB (Ben Johnston)
rodzic 38bdffbf5d
commit 01b36267af
2 zmienionych plików z 6 dodań i 10 usunięć

Wyświetl plik

@ -6,17 +6,13 @@ from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType
from django.core import checks
from django.db import DEFAULT_DB_ALIAS
from django.db.models import ForeignKey
from django.urls import reverse
from django.utils.module_loading import import_string
from wagtail.admin.checks import check_panels_in_model
from wagtail.admin.forms.models import register_form_field_override
from wagtail.admin.viewsets import viewsets
from wagtail.models import DraftStateMixin, LockableMixin, ReferenceIndex, WorkflowMixin
from .widgets import AdminSnippetChooser
SNIPPET_MODELS = []
@ -121,11 +117,6 @@ def _register_snippet_immediately(model, viewset=None):
errors = check_panels_in_model(model, "snippets")
return errors
# Set up admin model forms to use AdminSnippetChooser for any ForeignKey to this model
register_form_field_override(
ForeignKey, to=model, override={"widget": AdminSnippetChooser(model=model)}
)
def get_snippet_usage_url(self):
return reverse(

Wyświetl plik

@ -1,3 +1,4 @@
from django.utils.functional import cached_property
from django.utils.translation import gettext_lazy as _
from wagtail.admin.ui.tables import LiveStatusTagColumn
@ -11,6 +12,7 @@ from wagtail.admin.views.generic.chooser import (
)
from wagtail.admin.viewsets.chooser import ChooserViewSet
from wagtail.models import DraftStateMixin
from wagtail.snippets.widgets import AdminSnippetChooser
class BaseSnippetChooseView(BaseChooseView):
@ -64,8 +66,11 @@ class SnippetChosenMultipleView(ChosenMultipleView):
class SnippetChooserViewSet(ChooserViewSet):
register_widget = False # registering the snippet chooser widget for a given model is done in register_snippet
choose_view_class = ChooseView
choose_results_view_class = ChooseResultsView
chosen_view_class = SnippetChosenView
chosen_multiple_view_class = SnippetChosenMultipleView
@cached_property
def widget_class(self):
return AdminSnippetChooser(model=self.model)