【问题标题】:Creating Page object programmatically got error ValidationError path and depth fields cannot be blank/null以编程方式创建页面对象得到错误 ValidationError 路径和深度字段不能为空白/空
【发布时间】:2019-03-29 02:29:01
【问题描述】:

我正在尝试以编程方式创建一个PostPage 对象。这个类继承自 wagtail 的 Page 模型:

post = PostPage.objects.create(
    title='Dummy',
    intro='This is just for testing dummy',
    body='Lorem ipsum dolor...',
    first_published_at=datetime.strptime(f'2019-01-01', '%Y-%m-%d')
)

但是,我收到以下错误:

ValidationError: {'path': ['This field cannot be blank.'], 'depth': ['This field cannot be null.']}

我需要创建一些虚拟的Page 对象,所以我想知道如何解决这个问题。

【问题讨论】:

    标签: django wagtail


    【解决方案1】:

    我在 Google 网上论坛的 wagtail 支持 question 1question 2 找到了一些帮助我解决此问题的信息。基本上,我无法直接创建Page 对象,但我必须将其添加到另一个现有页面,如下所示:

    # assuming HomePage has at least one element
    home = HomePage.objects.all()[0]
    
    post = PostPage(
            title='Dummy',
            intro='This is just for testing dummy',
            body='Lorem ipsum dolor...',
            first_published_at=datetime.strptime(f'2019-01-01', '%Y-%m-%d'),
    )
    home.add_child(instance=post)
    home.save()
    

    这就像一个魅力!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多