Reputation: 11
I have tags model:
class MediaTag(TagBase):
free_tagging = False
color = models.CharField(
_("Tag color"),
max_length=7,
choices=MEDIA_TAG_COLOR_CHOICES
)
site = models.ForeignKey(
'wagtailcore.Site',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='+'
)
class MediaTagItem(ItemBase):
tag = models.ForeignKey(
MediaTag, related_name="tagged_media", on_delete=models.CASCADE
)
content_object = ParentalKey(
'MediaPage',
on_delete=models.CASCADE,
related_name='tagged_items'
)
class MediaPage(AbstractNewsPage, index.Indexed):
tags = ClusterTaggableManager(through=MediaTagItem, blank=False)
...
I need to change MediaPage admin widget for the tags feild to select. And set options for it with tags for the same site as the page. I can change the widget using the register_form_field_override function, but how do I pass choises there?
Upvotes: 0
Views: 69
Reputation: 11
It works, but only with single word tags:
from django import forms
from wagtail.admin.panels import FieldPanel
from django.apps import apps
class TagSelectPanel(FieldPanel):
def get_form_options(self, *args):
options = super().get_form_options(*args)
options["widgets"] = {
self.field_name: forms.widgets.Select(),
}
return options
def get_bound_panel(self, instance, *args, **kwargs):
panel = super().get_bound_panel(*args, **kwargs)
model = apps.get_model('news', 'MediaTag')
choices_queryset = model.objects.all()
field = panel.form.fields[self.field_name]
field.widget.choices = [
(item.name, item.name) for item in choices_queryset
]
return panel
class MediaPage(AbstractNewsPage, index.Indexed):
...
content_panels = AbstractNewsPage.content_panels + [
TagSelectPanel('tags'),
...
]
Upvotes: 1