为具有Waftail模型的CRUD创建具有从Page类继承的StreamField字段的DRF端点

时间:2019-02-25 10:34:40

标签: django-rest-framework wagtail

  1. 问题是如何看待DRF串行器? 因为不能使用PUT请求来创建新的Post(faq)。 Page类具有必填和必填的字段,但是如果需要将'slug','depth','title','path'放入请求中,我不明白如何创建新帖子。还有我可以带他们去的地方。
  2. StreamFields内部的字段被嵌入,并且我的DRF在此字段内返回的不是JSON,它返回行。也许有人已经做到了,可以提供帮助。谢谢。

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,并制作自定义管理部分

0 个答案:

没有答案