Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当wagtail BooleanField为true时启用wagtail DateField_Python_Django_Wagtail - Fatal编程技术网

Python 当wagtail BooleanField为true时启用wagtail DateField

Python 当wagtail BooleanField为true时启用wagtail DateField,python,django,wagtail,Python,Django,Wagtail,默认情况下,我需要禁用wagtail日期字段,但是如果内容作者选中了一个框(wagtail BooleanField),那么该字段应该启用并且是必需的。我正在努力找到解决这个问题的最佳方法,我还没有找到关于如何做到这一点的文档。我曾考虑使用Django信号或wagtail钩子,但对于我认为应该是常见用例的东西来说,这似乎是一个复杂的解决方案。所以我想知道是否有人有更好的选择或者给我指出正确的方向。我不相信这在Wagtail中是可能的(从Wagtail 2.2开始)。至少不是天生的 但是,您可以通

默认情况下,我需要禁用wagtail日期字段,但是如果内容作者选中了一个框(wagtail BooleanField),那么该字段应该启用并且是必需的。我正在努力找到解决这个问题的最佳方法,我还没有找到关于如何做到这一点的文档。我曾考虑使用Django信号或wagtail钩子,但对于我认为应该是常见用例的东西来说,这似乎是一个复杂的解决方案。所以我想知道是否有人有更好的选择或者给我指出正确的方向。

我不相信这在Wagtail中是可能的(从Wagtail 2.2开始)。至少不是天生的

但是,您可以通过以下代码段向管理员添加自定义JavaScript:

from django.contrib.staticfiles.templatetags.staticfiles import static
from django.utils.html import format_html

from wagtail.core import hooks


@hooks.register("insert_global_admin_js", order=100)
def global_admin_js():
    """Add custom.js to Wagtail Admin."""
    return format_html('<script src="{}"></script>', static("js/custom.js"))
从django.contrib.staticfiles.templatetags.staticfiles导入静态
从django.utils.html导入格式\u html
从wagtail.core导入钩子
@hooks.register(“insert\u global\u admin\u js”,order=100)
def global_admin_js():
“”“将custom.js添加到Wagtail管理。”“”
返回格式为html(“”,静态(“js/custom.js”))
然后在
custom.js
文件中,您可以添加一个JavaScript事件来检测布尔字段是否被选中,然后查找并选择日期字段并禁用或隐藏它


正如Willem Van Onsem所提到的,您需要将
日期字段
设置为空,以便在
日期字段
为空时保存页面(作为表单)

您不应该在模型中禁用它(事实上,您可以将
DateField
设为空,并删除
BooleanField
,但您需要在层中实现它(在JavaScript和表单实现中)。