Replies: 3 comments
-
This is a duplicate of #534 tl;dr there is nothing that would instruct the field panel as to what the current locale is and what to filter things on. You probably want to define your own |
Beta Was this translation helpful? Give feedback.
-
Thanks. I used #534 in the end because I couldn't wrap my head around filtering the snippet queryset. Its not an ideal solution and I'll keep looking into creating a custom |
Beta Was this translation helpful? Give feedback.
-
Turns out the locale is lurking in the Here's a select panel that will filter according to locale. It'll match the default panel type for the field, or you can override with an appropriate form widget (one of from django.core.exceptions import ImproperlyConfigured
from django.forms.models import ModelChoiceIterator
from django.forms.widgets import (CheckboxSelectMultiple, RadioSelect, Select,
SelectMultiple)
from django.utils.translation import gettext_lazy as _
from wagtail.admin.panels import FieldPanel
class LocalizedSelectPanel(FieldPanel):
"""
Customised FieldPanel to filter choices based on locale of page/model being created/edited
Usage:
widget_class - optional, override field widget type
- should be CheckboxSelectMultiple, RadioSelect, Select or SelectMultiple
typed_choice_field - set to True with Select widget forces drop down list
"""
def __init__(self, field_name, widget_class=None, typed_choice_field=False, *args, **kwargs):
if not widget_class in [None, CheckboxSelectMultiple, RadioSelect, Select, SelectMultiple]:
raise ImproperlyConfigured(_(
"widget_class should be a Django form widget class of type "
"CheckboxSelectMultiple, RadioSelect, Select or SelectMultiple"
))
self.widget_class = widget_class
self.typed_choice_field = typed_choice_field
super().__init__(field_name, *args, **kwargs)
def clone_kwargs(self):
return {
'heading': self.heading,
'classname': self.classname,
'help_text': self.help_text,
'widget_class': self.widget_class,
'typed_choice_field': self.typed_choice_field,
'field_name': self.field_name,
}
class BoundPanel(FieldPanel.BoundPanel):
def __init__(self, **kwargs):
super().__init__(**kwargs)
if not self.panel.widget_class:
self.form.fields[self.field_name].widget.choices=self.choice_list
else:
self.form.fields[self.field_name].widget = self.panel.widget_class(choices=self.choice_list)
if self.panel.typed_choice_field:
self.form.fields[self.field_name].__class__.__name__ = 'typed_choice_field'
pass
@property
def choice_list(self):
self.form.fields[self.field_name].queryset = self.form.fields[self.field_name].queryset.filter(locale_id=self.instance.locale_id)
choices = ModelChoiceIterator(self.form.fields[self.field_name])
return choices So in your Activity class, you'd call this with
|
Beta Was this translation helpful? Give feedback.
-
I'm sure I must be doing something wrong here. When editing the original language of a page, all the snippets show values for every language.
This is how the model and snippet is constructed.
We're just about to expand to more than 2 languages and this will get crazy.
Beta Was this translation helpful? Give feedback.
All reactions