models.py
from wagtail.core.models import Page
class TranslatedField:
def __init__(self, en_field, ru_field, tr_field, nl_field):
self.en_field = en_field
self.ru_field = ru_field
self.tr_field = tr_field
self.nl_field = nl_field
def __get__(self, instance, owner):
if translation.get_language() == 'ru':
return getattr(instance, self.ru_field)
elif translation.get_language() == 'tr':
return getattr(instance, self.tr_field)
elif translation.get_language() == 'nl':
return getattr(instance, self.nl_field)
else:
return getattr(instance, self.en_field)
class FaqPage(Page):
header = models.BooleanField(default=True)
groups_en = StreamField(
[
('groups', blocks.ListBlock(blocks.StructBlock([
('group_name', blocks.RichTextBlock()),
('questions', blocks.ListBlock(blocks.StructBlock([
('question', blocks.RichTextBlock()),
('answer', blocks.RichTextBlock())
]))
)
]
)))
]
, blank=True)
groups_ru = StreamField(
[
('groups', blocks.ListBlock(blocks.StructBlock([
('group_name', blocks.RichTextBlock()),
('questions', blocks.ListBlock(blocks.StructBlock([
('question', blocks.RichTextBlock()),
('answer', blocks.RichTextBlock())
]))
)
]
)))
]
, blank=True)
groups_tr = StreamField(
[
('groups', blocks.ListBlock(blocks.StructBlock([
('group_name', blocks.RichTextBlock()),
('questions', blocks.ListBlock(blocks.StructBlock([
('question', blocks.RichTextBlock()),
('answer', blocks.RichTextBlock())
]))
),
]
)))
]
, blank=True)
groups_nl = StreamField(
[
('groups', blocks.ListBlock(blocks.StructBlock([
('group_name', blocks.RichTextBlock()),
('questions', blocks.ListBlock(blocks.StructBlock([
('question', blocks.RichTextBlock()),
('answer', blocks.RichTextBlock())
]))
),
]
)))
]
, blank=True)
groups = TranslatedField('groups_en', 'groups_ru', 'groups_tr', 'groups_nl')
translated_title_en = models.CharField(blank=True, max_length=100)
translated_title_ru = models.CharField(blank=True, max_length=100)
translated_title_tr = models.CharField(blank=True, max_length=100)
translated_title_nl = models.CharField(blank=True, max_length=100)
translated_title = TranslatedField('translated_title_en', 'translated_title_ru', 'translated_title_tr',
'translated_title_nl')
content_panels = Page.content_panels + [
FieldPanel('translated_title_en', classname='t_en'),
FieldPanel('translated_title_ru', classname='t_ru'),
FieldPanel('translated_title_tr', classname='t_tr'),
FieldPanel('translated_title_nl', classname='t_nl'),
StreamFieldPanel('groups_en'),
StreamFieldPanel('groups_ru'),
StreamFieldPanel('groups_tr'),
StreamFieldPanel('groups_nl'),
]
serializers.py
class FaqSerializer(serializers.ModelSerializer):
"""
This viewset automatically provides `list`, `create`, `retrieve`,
`update` and `destroy` actions.
"""
class Meta:
model = FaqPage
fields = [
'id',
'header',
# 'groups_en',
# 'groups_ru',
# 'groups_tr',
# 'groups_nl',
# 'translated_title_en',
# 'translated_title_ru',
# 'translated_title_tr',
# 'translated_title_nl',
'path', # i don't understand how it works in Page class
'depth', # and what should be here? and in title and slug
'title',
'slug',
]
我观看了有关[wa] https://www.youtube.com/watch?v=_X3-EODO9G4&lc=z22tzdwrnuuohry4l04t1aokgqo0yca153lhfeiq1je5rk0h00410.1551287462019827的教程,向作者问了几个问题,并了解我是否需要使用自定义管理部分。我将重写所有内容。并决定停止使用the,并制作自定义管理部分