form instance is binded to the widget to retrieve page instance

pull/11639/head
Abdelrahman 2024-02-13 04:02:57 +02:00
rodzic a3cb4a6903
commit 589b49d6bf
4 zmienionych plików z 22 dodań i 1 usunięć

Wyświetl plik

@ -34,6 +34,7 @@ export class PageChooser extends Chooser {
matchSubclass: this.opts.matchSubclass,
canChooseRoot: this.opts.canChooseRoot,
userPerms: this.opts.userPerms,
parentId: this.opts.parentId,
};
if (this.state && this.state.parentId) {
opts.parentId = this.state.parentId;

Wyświetl plik

@ -1,5 +1,6 @@
from django import forms
from django.conf import settings
from django.forms import models
from django.utils.translation import gettext as _
from django.utils.translation import ngettext
@ -153,6 +154,15 @@ class WagtailAdminPageForm(WagtailAdminModelForm):
self.parent_page = parent_page
# bind the parent page of this forms page to the PageChooser widget
for obj in self.fields.values():
if isinstance(obj, models.ModelChoiceField):
try:
obj.widget.form_instance = self
except AttributeError:
# Then propably it isn't a page chooser
pass
if not self.show_comments_toggle:
del self.fields["comment_notifications"]

Wyświetl plik

@ -295,6 +295,9 @@ class BrowseView(View):
active_locale_id = selected_locale.pk
else:
active_locale_id = Locale.get_active().pk
selected_locale = get_object_or_404(
Locale, language_code=active_locale_id
)
# we are at the Root level, so get the locales from the current pages
choose_url = reverse("wagtailadmin_choose_page")

Wyświetl plik

@ -224,6 +224,7 @@ class AdminPageChooser(BaseChooser):
icon = "doc-empty-inverse"
classname = "page-chooser"
js_constructor = "PageChooser"
form_instance = None
def __init__(
self, target_models=None, can_choose_root=False, user_perms=None, **kwargs
@ -298,9 +299,15 @@ class AdminPageChooser(BaseChooser):
def get_js_init_options(self, id_, name, value_data):
opts = super().get_js_init_options(id_, name, value_data)
value_data = value_data or {}
# the parentId is set to the current instances id, if the current page instance
# exists i.e. EditView, else set to the parent page id since the current
# instance doesn't exist yet. i.e CreateView
page_id = self.form_instance.instance.id or self.form_instance.parent_page.id
parent_id = value_data.get("parent_id")
if parent_id is not None:
opts["parentId"] = parent_id
opts["parentId"] = parent_id or page_id
return opts
@property