# 模型参考 ```{eval-rst} .. module:: wagtail.models ``` 本文包含 `wagtail.models` 模块中各模型类的参考资料。 (page_model_ref)= ## `Page` ### 数据库字段 ```{eval-rst} .. class:: AbstractPage :canonical: wagtail.models.pages.AbstractPage .. attribute:: title (文本) 页面的可读标题。 .. attribute:: draft_title (文本) 页面的可读标题,会包含草稿编辑中做出的任何改动(与 ``title`` 字段不同 —— 对已发布的页面,后者是当前已发布版本中存在的标题)。 .. attribute:: slug (文本) 用于构造页面的 URL。 例如:``https://domain.com/blog/[my-slug]/`` .. attribute:: content_type (外键,指向 ``django.contrib.contenttypes.models.ContentType``) 指向 :class:`~django.contrib.contenttypes.models.ContentType` 对象的外键,该对象表示该页面所属的具体模型。 .. attribute:: live (布尔值) 当页面已发布时,该布尔值为 ``True``。 注意:该字段默认为 ``True``,意味着任何通过程序创建的页面默认都会发布。 .. attribute:: has_unpublished_changes (布尔值) 当页面处于草稿状态、或已发布但带草稿改动时,该布尔值为 ``True``。 .. attribute:: owner (外键,指向用户模型) 指向创建该页面的用户的外键。 .. attribute:: first_published_at (日期/时间) 页面首次发布的日期/时间。 .. attribute:: last_published_at (日期/时间) 页面最后发布的日期/时间。 .. attribute:: locked (布尔值) 设为 ``True`` 时,Wagtail 编辑器不允许任何用户编辑该页面的内容。 如果同时设置了 ``locked_by``,则只有该用户可以编辑该页面。 .. attribute:: locked_by (外键,指向用户模型) 当前锁定该页面的用户。只有该用户可以编辑该页面。 如果 ``locked`` 为 ``True`` 而此字段为 ``None``,则没有人能编辑该页面。 .. attribute:: locked_at (日期/时间) 页面被锁定的日期/时间。 .. attribute:: alias_of (外键,指向另一个页面) 若已设置,则该页面是本字段所引用页面的别名。 .. attribute:: locale (外键,指向 Locale) 该外键链接到表示该页面语言的 ``Locale`` 对象。 .. attribute:: translation_key (uuid) 在页面的各译文之间共享的 UUID。它在创建新页面时随机生成,并在创建页面译文时被复制。 一个 ``translation_key`` 值在每个区域中只能用于一个页面。 .. class:: ShowInMenusMixin .. attribute:: show_in_menus (布尔值) 控制该页面是否应包含在站点范围的菜单中,它显示在页面编辑器的 ``promote_panels`` 里。 Wagtail 默认不包含任何菜单实现,这意味着除非在具体的 Wagtail 安装中按那种方式实现,否则该字段不会对前端内容产生任何影响。 不过,:meth:`~wagtail.query.PageQuerySet.in_menu` 这个 QuerySet 过滤器会用到它,以便更容易地查询使用该字段的页面。 默认为 ``False``,可以在模型上用 ``show_in_menus_default = True`` 覆盖。 .. note:: 要为所有页面设置全局默认值,请在首次导入 ``Page`` 模型的地方设置一次 ``Page.show_in_menus_default = True``。 .. class:: DefaultBasePageMixin .. attribute:: seo_title (文本) 另行为 SEO 拟定的标题,用于页面的 ```` HTML 标签。 .. attribute:: search_description (文本) 为 SEO 拟定的内容描述,用于搜索索引。它也适合用作页面的 ``<meta name="description">`` HTML 标签。 .. class:: Page 所有页面类型都继承自的标准基础页面模型,除非用 :ref:`wagtail_page_model` 覆盖(见 :ref:`custom_page_models`)。 ``` ### 方法与属性 除所提供的模型字段之外,`Page` 还有许多属性与方法,你可能想在创建自己的模型时参考、使用或覆盖它们。 ```{note} 另见 [django-treebeard](inv:treebeard:std:doc#index) 的 [节点 API](inv:treebeard:std:doc#api)。``Page`` 是[物化路径树](inv:treebeard:std:doc#mp_tree)节点的子类。 ``` ```{eval-rst} .. class:: AbstractPage :no-index: .. automethod:: get_specific .. autoattribute:: specific .. autoattribute:: specific_deferred .. autoattribute:: specific_class .. automethod:: get_base_page .. autoattribute:: cached_content_type .. autoattribute:: page_type_display_name .. automethod:: get_url .. automethod:: get_full_url .. autoattribute:: full_url .. automethod:: relative_url .. automethod:: get_site .. automethod:: get_url_parts .. automethod:: route .. automethod:: serve .. automethod:: route_for_request .. automethod:: find_for_request .. method:: get_default_privacy_setting(request) 设置该页面的默认隐私设置。 该方法必须返回一个至少包含 'type' 键的字典。其值必须是 :class:`~wagtail.models.PageViewRestriction` 的 :attr:`~wagtail.models.PageViewRestriction.restriction_type` 中下列取值之一: - ``BaseViewRestriction.NONE``:页面是公开的,任何人都可以访问。(默认)- '{"type": BaseViewRestriction.NONE}' - ``BaseViewRestriction.LOGIN``:页面是私有的,只有已认证用户才能访问。- '{"type": BaseViewRestriction.LOGIN}' - ``BaseViewRestriction.PASSWORD``:页面是私有的,只有持有共享密码的用户才能访问。(需要在字典中额外提供 ``password`` 键)- '{"type": BaseViewRestriction.PASSWORD, "password": "P@ssw0rd123!"}' - ``BaseViewRestriction.GROUPS``:页面是私有的,只有特定用户组中的用户才能访问。(需要额外提供 ``groups`` 键,其值为 Group 对象列表)- '{"type": BaseViewRestriction.GROUPS, "groups": [moderators, editors]}' 示例 .. code-block:: python class BreadsIndexPage(Page): #... def get_default_privacy_setting(request): from wagtail.models import BaseViewRestriction # 如果编辑者拥有 foo.add_bar 权限,则把默认值设为勾选 moderators 与 editors 两个组的用户组限制 if request.user.has_perm("foo.add_bar"): moderators = Group.objects.filter(name="Moderators").first() editors = Group.objects.filter(name="Editors").first() return {"type": BaseViewRestriction.GROUPS, "groups": [moderators,editors]} else: return {"type": BaseViewRestriction.NONE} .. autoattribute:: context_object_name 页面 ``Context`` 中页面实例的自定义名称。 .. automethod:: get_context .. automethod:: get_template .. automethod:: get_admin_display_title .. autoattribute:: allowed_http_methods 定制该属性时,建议开发者使用 Python 内置 ``http.HTTPMethod`` 枚举中的值,因为它更稳健,并且使用的是内存中已有的值。例如: .. code-block:: python from http import HTTPMethod class MyPage(Page): allowed_http_methods = [HTTPMethod.GET, HTTPMethod.OPTIONS] ``http.HTTPMethod`` 枚举直到 Python 3.11 才加入,因此如果你的项目使用更早版本的 Python,可以改用大写字符串。例如: .. code-block:: python class MyPage(Page): allowed_http_methods = ["GET", "OPTIONS"] .. automethod:: check_request_method .. automethod:: handle_options_request .. autoattribute:: response_media_types 该页面类型的实例可以响应的媒体类型,例如 ``["text/html", "text/markdown"]``。第一项是默认值。当有多项时,:meth:`get_response_media_type` 会依据每个请求的 ``Accept`` 头为其挑选一项,并把 ``Accept`` 加入响应的 ``Vary`` 头。见 :ref:`varying_page_responses`。 .. automethod:: get_response_media_type .. automethod:: get_vary_headers .. autoattribute:: preview_modes .. autoattribute:: default_preview_mode .. autoattribute:: preview_sizes .. autoattribute:: default_preview_size .. automethod:: serve_preview .. automethod:: get_parent .. automethod:: get_children .. automethod:: get_ancestors .. automethod:: get_descendants .. automethod:: get_siblings .. automethod:: get_translations .. automethod:: get_translation .. automethod:: get_translation_or_none .. automethod:: has_translation .. automethod:: copy_for_translation .. method:: get_admin_default_ordering 返回在管理后台页面索引中查看子页面、且未处于搜索结果状态时,子页面默认的排序方式。 可用的排序方式有: - ``'content_type'`` - ``'-content_type'`` - ``'latest_revision_created_at'`` - ``'-latest_revision_created_at'`` - ``'live'`` - ``'-live'`` - ``'ord'`` - ``'title'`` - ``'-title'`` 例如,让某页面的所有子页面仅在数量小于 20 个时按标题排序。 .. code-block:: python class BreadsIndexPage(Page): def get_admin_default_ordering(self): if Page.objects.child_of(self).count() < 20: return 'title' return self.admin_default_ordering .. attribute:: admin_default_ordering 方法 ``get_admin_default_ordering()`` 对应的属性版本,默认为 ``'-latest_revision_created_at'``。 .. autoattribute:: localized .. autoattribute:: localized_draft .. attribute:: search_fields 要由搜索引擎建立索引的字段列表。见搜索文档 :ref:`wagtailsearch_indexing_fields` .. attribute:: subpage_types 可以作为该页面类型子页面创建的页面模型列表。例如,``BlogIndex`` 页面可以允许 ``BlogPage`` 作为子页面,但不允许 ``JobPage``: .. code-block:: python class BlogIndex(Page): subpage_types = ['mysite.BlogPage', 'mysite.BlogArchivePage'] 把某个页面的 subpage_types 属性设为空数组,可以完全禁止为它创建子页面: .. code-block:: python class BlogPage(Page): subpage_types = [] .. attribute:: parent_page_types 允许作为父页面的页面模型列表。例如,``BlogPage`` 可能只允许自己在 ``BlogIndex`` 页面下创建: .. code-block:: python class BlogPage(Page): parent_page_types = ['mysite.BlogIndexPage'] 把 parent_page_types 设为空数组,页面就可以完全禁止自己被创建(这对于创建只应存在一次的唯一页面很有用): .. code-block:: python class HiddenPage(Page): parent_page_types = [] 要让页面只能创建在根页面之下(例如针对 ``HomePage`` 模型),把 ``parent_page_type`` 设为 ``['wagtailcore.Page']``。 .. code-block:: python class HomePage(Page): parent_page_types = ['wagtailcore.Page'] .. automethod:: can_exist_under .. automethod:: can_create_at .. automethod:: can_move_to .. automethod:: get_route_paths .. attribute:: password_required_template 定义使用该模型的受保护页面在渲染登录表单时应使用哪个模板文件。这会覆盖你在设置中用 ``WAGTAIL_PASSWORD_REQUIRED_TEMPLATE`` 定义的默认值。见 :ref:`private_pages` .. attribute:: is_creatable 控制该页面能否通过 Wagtail 管理后台创建。默认为 ``True``,且不被子类继承。这在使用 :ref:`多表继承 <django:meta-and-multi-table-inheritance>` 时很有用,可防止基础模型被当作实际页面创建。 .. attribute:: max_count 控制通过 Wagtail 管理界面可以创建该类型页面的最大数量。当需要「这类页面最多只允许存在 3 个」、或需要单例页面时,这很有用。 .. attribute:: max_count_per_parent 控制该类型页面在任意单个父页面下可以创建的最大数量。 .. attribute:: private_page_options 控制该页面类型可用的隐私选项。 可用的选项有: - ``'password'`` - 可限制为使用共享密码 - ``'groups'`` - 可限制为特定用户组中的用户 - ``'login'`` - 可限制为已登录用户 .. code-block:: python class BreadPage(Page): ... # 默认 private_page_options = ['password', 'groups', 'login'] # 禁用共享密码 private_page_options = ['groups', 'login'] # 只用共享密码 private_page_options = ['password'] # 该页面模型没有任何隐私选项 private_page_options = [] .. attribute:: exclude_fields_in_copy 复制 Page 时不会被包含的字段名数组。 当你有些不使用 ``ClusterableModel``、或不应被复制的关系时很有用。 .. code-block:: python class BlogPage(Page): exclude_fields_in_copy = ['special_relation', 'custom_uuid'] 以下字段在复制时始终会被排除 - `['id', 'path', 'depth', 'numchild', 'url_path', 'path']`。 .. attribute:: base_form_class 该表单类被用作在 Wagtail 页面编辑器中编辑该类型页面的基础。 可以在模型上设置该属性以定制页面编辑器表单。 表单必须是 :class:`~wagtail.admin.forms.WagtailAdminPageForm` 的子类。 更多信息见 :ref:`custom_edit_handler_forms`。 .. automethod:: with_content_json .. automethod:: save .. automethod:: copy .. method:: move(new_parent, pos=None) 把页面及其所有后代移动到一个新的父页面下。 更多信息见 :meth:`django-treebeard <treebeard.models.Node.move>`。 .. automethod:: create_alias .. automethod:: update_aliases .. automethod:: get_cache_key_components .. autoattribute:: cache_key ``` (site_model_ref)= ## `Site` `Site` 模型对多站点安装很有用,它让管理员可以配置服务器响应的每个主机名该使用页面树的哪一部分。 {meth}`~wagtail.models.Site.find_for_request` 函数返回将处理给定 HTTP 请求的 Site 对象。 ### 数据库字段 ```{eval-rst} .. class:: Site .. attribute:: hostname (文本) 站点的主机名,不含协议、端口与路径。 例如:``www.mysite.com`` .. note:: 如果你想知道如何获取站点的根 URL,请使用 :attr:`~Site.root_url` 属性。 .. attribute:: port (数字) 站点响应所用的端口号。 .. attribute:: site_name (文本 - 可选) 站点的可读名称。Wagtail 本身不使用它,但它适合用在站点前端,例如 ``<title>`` 元素中。 例如:``Rod's World of Birds`` .. attribute:: root_page (外键,指向 :class:`~wagtail.models.Page`) 指向站点根页面的链接。该页面会出现在站点的 ``/`` URL 上,通常是一个首页。 .. attribute:: is_default_site (布尔值) 若该站点是默认站点,则为 ``True``。只有一个站点可以是默认站点。 当找不到具有所需主机名/端口的站点时,会使用默认站点作为回退。 ``` ### 方法与属性 ```{eval-rst} .. class:: Site :no-index: .. automethod:: find_for_request .. autoattribute:: root_url 返回站点的 URL。它由 :attr:`~Site.hostname` 与 :attr:`~Site.port` 字段计算得出。 URL 的协议部分依据 :attr:`~Site.port` 字段的值计算: - 80 = ``http://`` - 443 = ``https://`` - 其它值都会使用 ``http://`` 协议,端口会追加到主机名末尾(例如 ``http://mysite.com:8000/``) .. automethod:: get_site_root_paths ``` (locale_model_ref)= ## `Locale` `Locale` 模型定义站点上可用的语言和/或区域集合。 每条 `Locale` 记录都对应 {ref}`wagtail_content_languages_setting` 设置中定义的一个「语言代码」。 Wagtail 最初会建立一个 `Locale` 作为所有既有内容的默认语言。 这第一个区域会自动从 `WAGTAIL_CONTENT_LANGUAGES` 中选择与 `LANGUAGE_CODE` 中所定义的站点主要语言代码最接近的值。 如果之后改动了主要语言代码,Wagtail **不会**自动创建新的 `Locale` 记录,也不会更新已有的记录。 在启用国际化之前,所有页面都使用这个主 `Locale` 记录。 这样做是为了满足数据库约束,也让日后开启国际化更容易。 ### 更改 `WAGTAIL_CONTENT_LANGUAGES` 可以随时间推移在 `WAGTAIL_CONTENT_LANGUAGES` 中增删语言。 在从 `WAGTAIL_CONTENT_LANGUAGES` 中移除某个选项之前,重要的是把对应的 `Locale` 记录更新为使用另一种内容语言,或者删除它。 任何内容语言无效的 `Locale` 实例都会被自动从所有数据库查询中过滤掉,使其无法被编辑或查看。 ### 方法与属性 ```{eval-rst} .. class:: Locale .. autoattribute:: language_code .. automethod:: get_default .. automethod:: get_active .. autoattribute:: language_name .. autoattribute:: language_name_local .. autoattribute:: language_name_localized .. autoattribute:: is_default .. autoattribute:: is_active .. autoattribute:: is_bidi .. automethod:: get_display_name ``` ## `TranslatableMixin` `TranslatableMixin` 是一个抽象模型,可以加到任何非页面的 Django 模型上,使其可翻译。 页面已经包含该 mixin,因此无需再加。 非页面模型若要在管理后台中可翻译,还必须[注册为 Snippet](wagtailsnippets_registering)。另见 [](translatable_snippets)。 ### 数据库字段 ```{eval-rst} .. class:: TranslatableMixin .. attribute:: locale (外键,指向 :class:`wagtail.models.Locale`) 对页面而言,它默认为父页面的区域。 .. attribute:: translation_key (uuid) 每当创建新的模型实例时随机生成的 UUID。 它与该实例的所有译文共享,因此可用于查询译文。 ``` `translation_key` 与 `locale` 两个字段有唯一键约束,以防止该对象被多次翻译到同一语言。 ```{note} 目前这一点是通过 `TranslatableMixin.Meta` 中的 {attr}`~django.db.models.Options.unique_together` 强制实现的,但将来可能会替换为 `TranslatableMixin.Meta.constraints` 中的 {class}`~django.db.models.UniqueConstraint`。 如果你的模型定义了 [`Meta` 类](inv:django#ref/models/options)(无论是新定义,还是显式继承 `TranslatableMixin.Meta`),在设置 `unique_together` 或 {attr}`~django.db.models.Options.constraints` 时要留意。确保在 `translation_key` 与 `locale` 上要么有 `unique_together`、要么有 `UniqueConstraint`(不要两者都有)。对此有一项系统检查。 ``` ### 方法与属性 ```{eval-rst} .. class:: TranslatableMixin :no-index: .. automethod:: get_translations .. automethod:: get_translation .. automethod:: get_translation_or_none .. automethod:: has_translation .. automethod:: copy_for_translation .. automethod:: get_translation_model .. autoattribute:: localized ``` ## `PreviewableMixin` `PreviewableMixin` 是一个 mixin 类,可以加到任何非页面的 Django 模型上,以允许预览其实例。 页面已经包含该 mixin,因此无需再加。 非页面模型若要在管理后台中可预览,还必须[注册为 Snippet](wagtailsnippets_registering)。另见 [](wagtailsnippets_making_snippets_previewable)。 ### 方法与属性 ```{eval-rst} .. class:: PreviewableMixin .. autoattribute:: preview_modes .. autoattribute:: default_preview_mode .. autoattribute:: preview_sizes .. autoattribute:: default_preview_size .. automethod:: is_previewable .. automethod:: get_preview_context .. automethod:: get_preview_template .. automethod:: serve_preview ``` ## `RevisionMixin` `RevisionMixin` 是一个抽象模型,可以加到任何非页面的 Django 模型上,以允许保存其实例的修订版本。 页面已经包含该 mixin,因此无需再加。 非页面模型若要在管理后台中可保存修订版本,还必须[注册为 Snippet](wagtailsnippets_registering)。另见 [](wagtailsnippets_saving_revisions_of_snippets)。 ### 数据库字段 ```{eval-rst} .. class:: RevisionMixin .. attribute:: latest_revision (外键,指向 :class:`~wagtail.models.Revision`) 指向为该对象创建的最新修订版本。该引用存于数据库中,以优化性能。 ``` ### 方法与属性 ```{eval-rst} .. class:: RevisionMixin :no-index: .. autoattribute:: _revisions .. autoattribute:: revisions .. automethod:: save_revision .. automethod:: get_latest_revision_as_object .. automethod:: with_content_json ``` ## `DraftStateMixin` `DraftStateMixin` 是一个抽象模型,可以加到任何非页面的 Django 模型上,以允许其实例拥有未发布的改动。 该 mixin 要求同时应用 {class}`~wagtail.models.RevisionMixin`。页面已经包含该 mixin,因此无需再加。 非页面模型若要在管理后台中拥有发布相关功能,还必须[注册为 Snippet](wagtailsnippets_registering)。另见 [](wagtailsnippets_saving_draft_changes_of_snippets)。 ### 数据库字段 ```{eval-rst} .. class:: DraftStateMixin .. attribute:: live (布尔值) 当对象已发布时,该布尔值为 ``True``。 注意:该字段默认为 ``True``,意味着任何通过程序创建的对象默认都会发布。 .. attribute:: live_revision (外键,指向 :class:`~wagtail.models.Revision`) 指向当前生效的修订版本。 .. attribute:: has_unpublished_changes (布尔值) 当对象处于草稿状态、或已发布但带草稿改动时,该布尔值为 ``True``。 .. attribute:: first_published_at (日期/时间) 对象首次发布的日期/时间。 .. attribute:: last_published_at (日期/时间) 对象最后发布的日期/时间。 ``` ### 方法与属性 ```{eval-rst} .. class:: DraftStateMixin :no-index: .. automethod:: publish .. automethod:: unpublish .. automethod:: with_content_json ``` ## `LockableMixin` `LockableMixin` 是一个抽象模型,可以加到任何非页面的 Django 模型上,以允许其实例被锁定。 页面已经包含该 mixin,因此无需再加。更多细节见 [](wagtailsnippets_locking_snippets)。 非页面模型若要在管理后台中可锁定,还必须[注册为 Snippet](wagtailsnippets_registering)。另见 [](wagtailsnippets_locking_snippets)。 ### 数据库字段 ```{eval-rst} .. class:: LockableMixin .. attribute:: locked (布尔值) 当对象被锁定时,该布尔值为 ``True``。 .. attribute:: locked_at (日期/时间) 对象被锁定的日期/时间。 .. attribute:: locked_by (外键,指向用户模型) 锁定该对象的用户。 ``` ### 方法与属性 ```{eval-rst} .. class:: LockableMixin :no-index: .. automethod:: get_lock .. automethod:: with_content_json ``` ## `WorkflowMixin` `WorkflowMixin` 是一个 mixin 类,可以加到任何非页面的 Django 模型上,以允许其实例被提交到工作流。 该 mixin 要求同时应用 {class}`~wagtail.models.RevisionMixin` 与 {class}`~wagtail.models.DraftStateMixin`。页面已经包含该 mixin,因此无需再加。更多细节见 [](wagtailsnippets_enabling_workflows)。 非页面模型若要在管理后台中拥有工作流功能,还必须[注册为 Snippet](wagtailsnippets_registering)。另见 [](wagtailsnippets_enabling_workflows)。 ### 方法与属性 ```{eval-rst} .. class:: WorkflowMixin .. automethod:: get_default_workflow .. autoattribute:: has_workflow .. automethod:: get_workflow .. autoattribute:: _workflow_states .. autoattribute:: workflow_states .. autoattribute:: workflow_in_progress .. autoattribute:: current_workflow_state .. autoattribute:: current_workflow_task_state .. autoattribute:: current_workflow_task ``` (revision_model_ref)= ## `Revision` 每次编辑页面都会创建一个新的 `Revision` 并保存到数据库。它可用于查找页面所做全部改动的完整历史,也为新改动在上线之前提供了一个存放之处。 - 可以从任何 {class}`~wagtail.models.RevisionMixin` 实例调用其 {meth}`~RevisionMixin.save_revision` 方法来创建修订版本。 - 页面内容可 JSON 序列化,存储在 {attr}`~Revision.content` 字段中。 - 你可以通过调用 {meth}`~Revision.as_object` 方法,把 `Revision` 取为该对象模型的一个实例。 你可以使用 [`purge_revisions`](purge_revisions) 命令删除不再使用的旧修订版本。 ### 数据库字段 ```{eval-rst} .. class:: Revision .. attribute:: content_object (通用外键) 该修订版本所属的对象。对页面修订版本而言,该对象是具体页面类的实例。 .. attribute:: content_type (外键,指向 :class:`~django.contrib.contenttypes.models.ContentType`) 该修订版本所属对象的内容类型。对页面修订版本而言,指具体页面类型的内容类型。 .. attribute:: base_content_type (外键,指向 :class:`~django.contrib.contenttypes.models.ContentType`) 该修订版本所属对象的基础内容类型。对页面修订版本而言,指 :class:`~wagtail.models.Page` 模型的内容类型。 .. attribute:: object_id (字符串) 该修订版本所属对象的主键。 .. attribute:: created_at (日期/时间) 创建该修订版本的时间。 .. attribute:: user (外键,指向用户模型) 创建该修订版本的用户。 .. attribute:: content (字典) 创建该修订版本时对象所对应的 JSON 内容。 ``` ### 管理器 ```{eval-rst} .. class:: Revision :no-index: .. attribute:: objects 这个默认管理器用于取回数据库中的所有 ``Revision`` 对象。它还提供 ``wagtail.models.RevisionsManager.for_instance`` 方法,让你可以查询特定对象的修订版本。 示例: .. code-block:: python Revision.objects.all() Revision.objects.for_instance(my_object) .. attribute:: page_revisions 该管理器扩展了默认管理器,用于取回所有属于页面的 ``Revision`` 对象。 示例: .. code-block:: python Revision.page_revisions.all() ``` ### 方法与属性 ```{eval-rst} .. class:: Revision :no-index: .. automethod:: as_object 该方法把此修订版本取为其所属对象的具体类的一个实例。若该修订版本属于页面,则为 :class:`~wagtail.models.Page` 相应具体子类的实例。 .. automethod:: is_latest_revision 若此修订版本是该对象的最新修订版本,则返回 ``True``。 .. automethod:: publish 调用它会把此修订版本的内容复制到生效对象中。若该对象处于草稿状态,它会被发布。 .. autoattribute:: base_content_object 该属性返回此修订版本所属对象,作为基类的实例。 ``` ## `GroupPagePermission` ### 数据库字段 ```{eval-rst} .. class:: GroupPagePermission .. attribute:: group (外键,指向 ``django.contrib.auth.models.Group``) .. attribute:: page (外键,指向 :class:`~wagtail.models.Page`) ``` ## `PageViewRestriction` ### 数据库字段 ```{eval-rst} .. class:: PageViewRestriction .. attribute:: page (外键,指向 :class:`~wagtail.models.Page`) .. attribute:: password (文本) .. attribute:: restriction_type (文本) 可选值:none、password、groups、login ``` ## `Orderable`(抽象) ### 数据库字段 ```{eval-rst} .. class:: Orderable .. attribute:: sort_order (数字) .. autoattribute:: sort_order_field ``` ## `Workflow` 工作流表示一系列任务,必须全部通过后才能对某个对象执行某项操作 —— 通常是发布。 ### 数据库字段 ```{eval-rst} .. class:: Workflow .. attribute:: name (文本) 工作流的可读名称。 .. attribute:: active (布尔值) 工作流是否启用。启用的工作流可以添加到页面与 Snippet 上并启动。未启用的则不行。 ``` ### 方法与属性 ```{eval-rst} .. class:: Workflow :no-index: .. automethod:: start .. autoattribute:: tasks .. automethod:: deactivate .. automethod:: all_pages ``` ## `WorkflowState` 工作流状态表示某个已在对象上启动的工作流的状况。 ### 数据库字段 ```{eval-rst} .. class:: WorkflowState .. attribute:: content_object (通用外键) 已在其上启动工作流的对象。对页面工作流而言,该对象是基础 ``Page`` 模型的实例。 .. attribute:: content_type (外键,指向 :class:`~django.contrib.contenttypes.models.ContentType`) 该工作流状态所属对象的内容类型。对页面工作流而言,指具体页面类型的内容类型。 .. attribute:: base_content_type (外键,指向 :class:`~django.contrib.contenttypes.models.ContentType`) 该工作流状态所属对象的基础内容类型。对页面工作流而言,指 :class:`~wagtail.models.Page` 模型的内容类型。 .. attribute:: object_id (字符串) 该修订版本所属对象的主键。 .. attribute:: workflow (外键,指向 ``Workflow``) 该 ``WorkflowState`` 所表示其状态的工作流。 .. attribute:: status (文本) 工作流的当前状态(可选值见 ``WorkflowState.STATUS_CHOICES``) .. attribute:: created_at (日期/时间) 该 ``WorkflowState`` 实例创建的时间 —— 即工作流启动的时间 .. attribute:: requested_by (外键,指向用户模型) 启动该工作流的用户 .. attribute:: current_task_state (外键,指向 ``TaskState``) 工作流当前所处任务对应的 ``TaskState`` 模型:若正在进行,则是正在完成的任务状态;若已结束,则是最终任务状态 ``` ### 方法与属性 ```{eval-rst} .. class:: WorkflowState :no-index: .. attribute:: STATUS_CHOICES ``status`` 字段可能取值及其可读名称组成的元组。可选值为 ``STATUS_IN_PROGRESS``、``STATUS_APPROVED``、 ``STATUS_CANCELLED`` 和 ``STATUS_NEEDS_CHANGES``。 .. automethod:: update .. automethod:: get_next_task .. automethod:: cancel .. automethod:: finish .. automethod:: resume .. automethod:: copy_approved_task_states_to_revision .. automethod:: all_tasks_with_status .. automethod:: revisions ``` ## `Task` 任务表示工作流中的各个阶段,必须全部通过,工作流才能成功完成。 ### 数据库字段 ```{eval-rst} .. class:: Task .. attribute:: name (文本) 任务的可读名称。 .. attribute:: active (布尔值) 任务是否启用:启用的任务可以加入工作流并启动。未启用的则不行,并且在 既有工作流中会被跳过。 .. attribute:: content_type (外键,指向 ``django.contrib.contenttypes.models.ContentType``) 指向 :class:`~django.contrib.contenttypes.models.ContentType` 对象的外键,该对象表示该任务所属的具体模型。 ``` ### 方法与属性 ```{eval-rst} .. class:: Task :no-index: .. autoattribute:: workflows .. autoattribute:: active_workflows .. attribute:: task_state_class 为该任务存储状态信息时要生成的具体任务状态类。若未指定,则为 ``TaskState``。 .. automethod:: get_verbose_name .. autoattribute:: specific .. automethod:: start .. automethod:: on_action .. automethod:: user_can_access_editor .. automethod:: user_can_lock .. automethod:: user_can_unlock .. automethod:: locked_for_user .. automethod:: get_actions .. automethod:: get_task_states_user_can_moderate .. automethod:: deactivate .. automethod:: get_form_for_action .. automethod:: get_template_for_action .. automethod:: get_description ``` ## `TaskState` 任务状态存储某个任务在特定修订版本上的进度状态信息。 ### 数据库字段 ```{eval-rst} .. class:: TaskState .. attribute:: workflow_state (外键,指向 :class:`~wagtail.models.WorkflowState`) 启动此任务状态的工作流状态。 .. attribute:: revision (外键,指向 :class:`~wagtail.models.Revision`) 创建此任务状态时所对应的修订版本。 .. attribute:: task (外键,指向 :class:`~wagtail.models.Task`) 此任务状态为其存储状态信息的任务。 .. attribute:: status (文本) 该修订版本上任务的完成状态。可选值见 ``TaskState.STATUS_CHOICES``) .. attribute:: content_type (外键,指向 ``django.contrib.contenttypes.models.ContentType``) 指向 :class:`~django.contrib.contenttypes.models.ContentType` 对象的外键,该对象表示该任务所属的具体模型。 .. attribute:: started_at (日期/时间) 创建此任务状态的时间。 .. attribute:: finished_at (日期/时间) 此任务状态被取消、拒绝或批准的时间。 .. attribute:: finished_by (外键,指向用户模型) 完成(取消、拒绝、批准)该任务的用户。 .. attribute:: comment (文本) 用户完成任务时通常会添加的文本评论。 ``` ### 方法与属性 ```{eval-rst} .. class:: TaskState :no-index: .. attribute:: STATUS_CHOICES ``status`` 字段可能取值及其可读名称组成的元组。可选值为 ``STATUS_IN_PROGRESS``、``STATUS_APPROVED``、 ``STATUS_CANCELLED``、``STATUS_REJECTED`` 和 ``STATUS_SKIPPED``。 .. attribute:: exclude_fields_in_copy 调用 ``TaskState.copy()`` 方法时不复制的字段列表。 .. autoattribute:: specific .. automethod:: approve .. automethod:: reject .. autoattribute:: task_type_started_at .. automethod:: cancel .. automethod:: copy .. automethod:: get_comment ``` ## `WorkflowTask` 表示某个任务在特定工作流中的排列顺序。 ### 数据库字段 ```{eval-rst} .. class:: WorkflowTask .. attribute:: workflow (外键,指向 ``Workflow``) .. attribute:: task (外键,指向 ``Task``) .. attribute:: sort_order (数字) 任务在工作流中的顺序。 ``` ## `WorkflowPage` 表示工作流对某个页面及其后代的指派。 ### 数据库字段 ```{eval-rst} .. class:: WorkflowPage .. attribute:: workflow (外键,指向 :class:`~wagtail.models.Workflow`) .. attribute:: page (外键,指向 :class:`~wagtail.models.Page`) ``` ## `WorkflowContentType` 表示工作流对某个 Django 模型的指派。 ### 数据库字段 ```{eval-rst} .. class:: WorkflowContentType .. attribute:: workflow (外键,指向 :class:`~wagtail.models.Workflow`) .. attribute:: content_type (外键,指向 :class:`~django.contrib.contenttypes.models.ContentType`) 指向 ``ContentType`` 对象的外键,该对象表示被指派到该工作流的模型。 ``` ## `BaseLogEntry` 一个抽象基类,表示在某个对象上执行某项操作的记录。 ### 数据库字段 ```{eval-rst} .. class:: BaseLogEntry .. attribute:: content_type (外键,指向 ``django.contrib.contenttypes.models.ContentType``) 指向 :class:`~django.contrib.contenttypes.models.ContentType` 对象的外键,该对象表示该模型所属的具体模型。 .. attribute:: label (文本) 创建该条目时对象的标题 注意:Wagtail 会尝试使用 ``get_admin_display_title``,或传给 ``LogEntryManager.log_action`` 的对象的字符串表示 .. attribute:: user (外键,指向用户模型) 指向触发该操作的用户的外键。 .. attribute:: revision (外键,指向 :class:`Revision`) 指向当前修订版本的外键。 .. attribute:: data (字典) 每项操作任何额外细节的 JSON 表示。 例如,从页面复制时的源页面 id 与标题,或工作流流转时的工作流 id/名称与下一步 id/名称 .. attribute:: timestamp (日期/时间) 创建该条目的日期/时间。 .. attribute:: content_changed (布尔值) 内容发生变化时可以设为 ``True`` 的布尔值。 .. attribute:: deleted (布尔值) 对象被删除时可以设为 ``True`` 的布尔值。用于在站点历史报告中过滤条目。 ``` ### 方法与属性 ```{eval-rst} .. class:: BaseLogEntry :no-index: .. autoattribute:: user_display_name .. autoattribute:: comment .. autoattribute:: object_verbose_name .. automethod:: object_id ``` ## `PageLogEntry` 表示在 {class}`Page` 上执行某项操作的记录,是 {class}`BaseLogEntry` 的子类。 ### 数据库字段 ```{eval-rst} .. class:: PageLogEntry .. attribute:: page (外键,指向 :class:`Page`) 指向执行该操作的页面的外键。 ``` ## `Comment` 表示页面上的评论。 ### 数据库字段 ```{eval-rst} .. class:: Comment .. attribute:: page (父键,指向 :class:`Page`) 指向该评论所加到的页面的父键。 .. attribute:: user (外键,指向用户模型) 指向添加该评论的用户的外键。 .. attribute:: text (文本) 评论的文本内容。 .. attribute:: contentpath (文本) 该评论所附着的字段或 StreamField 区块的路径, 形式为 ``field`` 或 ``field.streamfield_block_id``。 .. attribute:: position (文本) 该评论在其字段中位置的标识符。所用格式 由该字段决定。 .. attribute:: created_at (日期/时间) 创建该评论的日期/时间。 .. attribute:: updated_at (日期/时间) 更新该评论的日期/时间。 .. attribute:: revision_created (外键,指向 :class:`Revision`) 指向创建该评论时所对应修订版本的外键。 .. attribute:: resolved_at (日期/时间) 该评论被解决的日期/时间(若有)。 .. attribute:: resolved_by (外键,指向用户模型) 指向解决该评论的用户的外键(若有)。 ``` ## `CommentReply` 表示对某个评论串的回复。 ### 数据库字段 ```{eval-rst} .. class:: CommentReply .. attribute:: comment (父键,指向 :class:`Comment`) 指向开启该评论串的评论的父键。 .. attribute:: user (外键,指向用户模型) 指向添加该评论的用户的外键。 .. attribute:: text (文本) 评论的文本内容。 .. attribute:: created_at (日期/时间) 创建该评论的日期/时间。 .. attribute:: updated_at (日期/时间) 更新该评论的日期/时间。 ``` ## `PageSubscription` 表示某个用户对页面事件邮件通知的订阅。 目前仅用于评论通知。 ### 数据库字段 ```{eval-rst} .. class:: PageSubscription .. attribute:: page (父键,指向 :class:`Page`) .. attribute:: user (外键,指向用户模型) .. attribute:: comment_notifications (布尔值) 该用户是应接收所有评论的评论通知, 还是只接收其参与的评论串中的评论通知。 ```