Python 如何更新Wagtail页面

Python 如何更新Wagtail页面,python,django,wagtail,Python,Django,Wagtail,我使用wagtail_钩子更新页面对象,遇到了麻烦。更具体地说,当最终用户从浏览器点击“保存草稿”按钮时,我希望触发以下代码。此代码的目的是根据下面列出的条件语句的结果动态更改知识库id def sync_knowledge_base_page_with_zendesk2(request, page): if isinstance(page, ContentPage): page_instance = ContentPage.objects.get(pk=page.pk)

我使用wagtail_钩子更新页面对象,遇到了麻烦。更具体地说,当最终用户从浏览器点击“保存草稿”按钮时,我希望触发以下代码。此代码的目的是根据下面列出的条件语句的结果动态更改知识库id

def sync_knowledge_base_page_with_zendesk2(request, page):
    if isinstance(page, ContentPage):
        page_instance = ContentPage.objects.get(pk=page.pk)
        pageJsonHolder = page_instance.get_latest_revision().content_json
        content = json.loads(pageJsonHolder)
        print("content at the start = ")
        print(content['knowledge_base_id'])
        kb_id_revision = content['knowledge_base_id']
        kb_active_revision = content['kb_active']
        if kb_active_revision == "T":
            if kb_id_revision == 0:
                print("create the article")
                content['knowledge_base_id'] = 456
                #page_instance.knowledge_base_id = 456 # change this API call
            else:
                print("update the article")
        elif kb_id_revision != 0:
            print("delete the article")
            content['knowledge_base_id'] = 0
            #page_instance.knowledge_base_id = 0
        print("content at the end = ")
        print(content['knowledge_base_id'])
        #page_instance.save_revision().publish
因此,当钩子代码触发时,它会用除知识库id之外的所有信息更新草稿


但是,当我像这样更改知识库id时(参见上面的注释)

然后像这样保存它(也可以看到上面的注释)


它保存更新的知识库id,但跳过其他修订。简言之,我到底做错了什么。提前感谢您的帮助。保重,祝你度过愉快的一天。

所以我解决了这个问题。我没有尝试使用页面方法save_revisions(),而是选择使用revisions.create()。在revisions.create()中,您将向其传递一个JSON对象和更新的值。除此之外,您还将传递用户的一个实例,以及提交供审核和批准的值。下面列出的是我的更新代码示例和注释。如果你有任何问题,请告诉我。我希望这篇文章能帮助其他人避免因更新版本而感到沮丧。谢谢你的阅读。保重,祝你度过愉快的一天

from wagtail.wagtailcore import hooks
from .models import ContentPage
import json


# Allows the latest page revision JSON to be updated based on conditionals
def sync_kb_page_with_zendesk(request, page):

    # Sanity check to ensure page is an instance of ContentPage
    if isinstance(page, ContentPage):

        # this sets the user variable
        user_var = request.user 

        # sets the Content Page
        page_instance = ContentPage.objects.get(pk=page.pk) 

        # this retrieves JSON str w/ latest revisions
        pageJsonHolder = page_instance.get_latest_revision().content_json 

        # this takes the json string and converts it into a json object
        content = json.loads(pageJsonHolder) 

        # this sets the kb id variable for use in the code
        kb_id_revision = content['knowledge_base_id'] 

        # this sets the kb active variable for use in the code
        kb_active_revision = content['kb_active'] 

        # this is the conditional logic 
        if kb_active_revision == "T":
            if kb_id_revision == 0:
                print("create the article")
                # updates the kb id value in the JSON object
                content['knowledge_base_id'] = 456 
            else:
                print("update the article")
        elif kb_id_revision != 0:
            print("delete the article")
            # updates the kb id value in the JSON object
            content['knowledge_base_id'] = 0 

        # this takes the JSON object and coverts it back to a JSON str
        revisionPageJsonHolder = json.dumps(content) 

        # this takes your JSON str and creates the latest revision of Page 
        revision = page_instance.revisions.create(
            content_json=revisionPageJsonHolder,
            user=user_var,
            submitted_for_moderation=False,
            approved_go_live_at=None,
        ) 


# registers the function to fire after page edit
hooks.register('after_edit_page', sync_kb_page_with_zendesk) 

# registers the function to fire after page creation
hooks.register('after_create_page', sync_kb_page_with_zendesk)

您要将此函数附加到哪个钩子?hooks.register('after_edit_page',sync_knowledge_base_page_与\u zendesk2)hooks.register('after_create_page',sync_knowledge_base_page_与\u zendesk2)恐怕我无法从这个代码片段中提取特定于摇摆尾的细节-这段代码中似乎发生了很多事情(测试
kb\u active\u revision
,提及API调用…)这与您的问题没有直接关系。您能否将其细化为一个最小、完整且可验证的示例(),请?我想获取一个包含最新页面修订版信息的JSON对象。幸运的是,这一部分已经完成。根据条件语句的结果,我想更改其中一个键的一个值。这一部分也已经完成。我现在有一个JSON对象,其中包含所有正确的值。那么@gasman,我如何保存更新后的JSON对象ject作为最新版本,因此在页面刷新后,浏览器中显示的值?我尝试将JSON传递给save_revisions()方法,但遇到了问题。使用save_revisions.publish()将键值保存到模型,而不是上一个版本。
page_instance.save_revision().publish()
from wagtail.wagtailcore import hooks
from .models import ContentPage
import json


# Allows the latest page revision JSON to be updated based on conditionals
def sync_kb_page_with_zendesk(request, page):

    # Sanity check to ensure page is an instance of ContentPage
    if isinstance(page, ContentPage):

        # this sets the user variable
        user_var = request.user 

        # sets the Content Page
        page_instance = ContentPage.objects.get(pk=page.pk) 

        # this retrieves JSON str w/ latest revisions
        pageJsonHolder = page_instance.get_latest_revision().content_json 

        # this takes the json string and converts it into a json object
        content = json.loads(pageJsonHolder) 

        # this sets the kb id variable for use in the code
        kb_id_revision = content['knowledge_base_id'] 

        # this sets the kb active variable for use in the code
        kb_active_revision = content['kb_active'] 

        # this is the conditional logic 
        if kb_active_revision == "T":
            if kb_id_revision == 0:
                print("create the article")
                # updates the kb id value in the JSON object
                content['knowledge_base_id'] = 456 
            else:
                print("update the article")
        elif kb_id_revision != 0:
            print("delete the article")
            # updates the kb id value in the JSON object
            content['knowledge_base_id'] = 0 

        # this takes the JSON object and coverts it back to a JSON str
        revisionPageJsonHolder = json.dumps(content) 

        # this takes your JSON str and creates the latest revision of Page 
        revision = page_instance.revisions.create(
            content_json=revisionPageJsonHolder,
            user=user_var,
            submitted_for_moderation=False,
            approved_go_live_at=None,
        ) 


# registers the function to fire after page edit
hooks.register('after_edit_page', sync_kb_page_with_zendesk) 

# registers the function to fire after page creation
hooks.register('after_create_page', sync_kb_page_with_zendesk)