获取有关创建新的Wagtail页面的父页面

编程入门 行业动态 更新时间:2024-10-27 04:28:21
本文介绍了获取有关创建新的Wagtail页面的父页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在创建新页面时,我需要从父页面中给定值来更改某些字段默认值.在编辑现有页面时,这不是问题,但是在创建新页面时,我需要为字段设置默认值.我尝试覆盖管理表单,在初始化WagtailAdminPageForm时,parent_page参数为空.而且,如果我尝试在Page子类的init方法形式上执行此操作,则父信息将不包含arg或kwargs.

I need to change some field default value given a value from the parent page when creating a new page. When editing an existing page, this is not a problem, but when creating a new page, I need to set a default value for a field. I have tried overriding the admin form, on init for WagtailAdminPageForm, the parent_page parameter is empty. And if I try to do it on the init method form Page subclass, there is no arg or kwargs with the parent information.

有没有办法获取新页面将作为父页面的页面?

is there a way to get the page for which the new page will be the parent?

这是我在Page构造函数上尝试过的

This is what I tried on Page constructor

class CMSPage(Page): . . . def __init__(self, *args, **kwargs): super(BaseContentMixin, self).__init__(*args, **kwargs) if hasattr(self.get_parent().specific, 'published_site') and self.get_parent().specific.published_site == 'extranet': self.published_site = 'extranet'

这可以编辑页面,对于新页面,我得到NoneType对象没有特定的属性.

This works editing a page, for new page, I get that NoneType objects has no attribute specific.

Django版本是1.10,Python版本是3.6,Wagtail版本是1.9

Django version is 1.10, Python version is 3.6, Wagtail version is 1.9

推荐答案

在澄清问题之后,这里是第二个答案,可能更合适.请注意,此需要Wagtail 1.11.x ,目前尚未发布.

After clarification of the question, here is a second answer that might be more suitable. Note that this requires Wagtail 1.11.x which as at this time is not yet released.

首先为您的自定义页面表单创建新的类,通常在models.py或您页面的模型所在的任何位置.

First Create a new class for your custom page form, usually in models.py or wherever the model for your page is.

from wagtail.wagtailadmin.forms import WagtailAdminPageForm class MyCustomPageForm(WagtailAdminPageForm): # Override the __init__ function to update 'initial' form values def __init__(self, data=None, files=None, parent_page=None, *args, **kwargs): print('parent_page', parent_page, parent_page.id, parent_page.title) # update the kwargs BEFORE the init of the super form class instance = kwargs.get('instance') if not instance.id: # only update the initial value when creating a new page kwargs.update(initial={ # 'field': 'value' 'title': parent_page.id # can be anything from the parent page }) # Ensure you call the super class __init__ super(MyCustomPageForm, self).__init__(data, files, *args, **kwargs) self.parent_page = parent_page

第二在页面模型定义上,告诉该模型使用您定义的表单类

Second On the page model definition, tell that model to use the form class you have defined

from wagtail.wagtailcore.models import Page class MyCustomPage(Page): base_form_class = MyCustomPageForm

解决方案的说明

  • Wagtail提供了重写在Wagtail Admin中构建用于创建和编辑的表单时调用的表单类(注意:不是模型类)的功能.
  • 文档:自定义生成的表单
  • 我们的自定义表单类将继承 WagtailAdminPageForm 类.
  • 在这个新的表单类中,我们想定义我们自己的 __ init __ 函数,该函数在创建表单时调用.
  • 请注意默认定义的Github代码WagtailAdminPageForm上的 __ init __
  • 要注入自定义值,我们将在调用类的超级 __ init __
  • 之前覆盖 kwargs 中的 initial 数据
  • 我们只希望在创建新页面时执行此操作,因此请检查实例是否没有 id
  • 我们可以访问 parent_page 实例及其中的任何字段
  • 添加了自定义的初始数据后,我们使用更改的kwargs调用超级 __ init __
  • 注意:之所以要求Wagtail 1.11.x,是因为直到拉动请求3508
  • Explanation of Solution

    • Wagtail provides the ability to override the form class (note: not model class) that gets called when building the form for creation and editing in Wagtail Admin.
    • Documentation: Customising generated forms
    • Our custom form class will inherit the WagtailAdminPageForm class.
    • Inside this new form class, we want to define our own __init__ function, this is called when the form is created.
    • Note the Github code for the default definition of __init__ on WagtailAdminPageForm
    • To inject custom values, we will override the initial data in kwargs before the calling of the class' super __init__
    • We only want to do this when a new page is being created, so check that the instance has no id
    • We can access the parent_page instance and any fields in it
    • After adding our custom initial data, then we call the super __init__ with the changed kwargs
    • Note: The reason for the requirement of Wagtail 1.11.x is that previous versions until that point did not add parent_page when calling the class model until pull request 3508

更多推荐

获取有关创建新的Wagtail页面的父页面

本文发布于:2023-11-03 06:24:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1554443.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:页面   Wagtail

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!