主视图配置¶
如果要自定义实体的主视图,则可能不需要重写主视图类。对于简单的调整(属性或关系显示位置和样式),一种更简单的方法是使用uicfg。
属性/关系显示位置¶
在主视图中,有三个部分可以显示属性和关系(在上图中以粉红色表示):
'属性'
'关系'
'侧边栏'
- 属性 只能在“属性”部分中显示(默认
行为)。它们也可以被隐藏。默认情况下,类型的属性 Password 和 Bytes 是隐藏的。
例如,隐藏 title
的属性 Blog
实体:
from cubicweb.web.views import uicfg
uicfg.primaryview_section.tag_attribute(('Blog', 'title'), 'hidden')
关系 可以显示在三个部分之一,也可以隐藏。
对于关系,有两种方法:
tag_object_of
用于修改对象的主视图tag_subject_of
用于修改主题的主视图
这两种方法有两个参数:
三联体
(subject, relation_name, object)
,其中主题或对象可以替换为'*'
节名称或
hidden
pv_section = uicfg.primaryview_section
# hide every relation `entry_of` in the `Blog` primary view
pv_section.tag_object_of(('*', 'entry_of', 'Blog'), 'hidden')
# display `entry_of` relations in the `relations`
# section in the `BlogEntry` primary view
pv_section.tag_subject_of(('BlogEntry', 'entry_of', '*'), 'relations')
显示内容¶
你可以使用 primaryview_display_ctrl
自定义属性或关系的显示。的值 primaryview_display_ctrl
是字典。
属性和关系的通用键是:
vid
:指定用于显示属性或关系的视图的区域ID。- 如果
vid
未指定,默认值取决于节: attributes
部分:“重新编辑”视图relations
部分:“自动限制”视图sideboxes
部分:“侧边栏”视图
- 如果
order
:int用于控制节中的顺序。如果未指定,则根据添加标记的顺序自动设置。label
:关系部分或侧框的标签showlabel
:布尔值,指示是否显示标签
# let us remind the schema of a blog entry
class BlogEntry(EntityType):
title = String(required=True, fulltextindexed=True, maxsize=256)
publish_date = Date(default='TODAY')
content = String(required=True, fulltextindexed=True)
entry_of = SubjectRelation('Blog', cardinality='?*')
# now, we want to show attributes
# with an order different from that in the schema definition
view_ctrl = uicfg.primaryview_display_ctrl
for index, attr in enumerate('title', 'content', 'publish_date'):
view_ctrl.tag_attribute(('BlogEntry', attr), {'order': index})
默认情况下,“关系”部分中显示的关系由“自动限制”视图显示。此视图将使用逗号分隔的值,或者列表视图和/或限制您的RSET(如果其中有太多的项目)(在本例中生成“查看全部”链接)。
您可以通过在 primaryview_display_ctrl 关系标记:
limit ,要显示的最大实体数。默认情况下使用“navigation.relevant limit”cwproperty的值(默认为8)。如果没有,就没有限制。
use_list_limit ,直到它们显示为列表的实体数(例如使用“列表”视图)。低于该限制,将使用“csv”视图。如果没有,仍然使用“csv”显示。
subvid ,子视图标识符(例如列表中每个项应使用的视图)
注意,您也可以使用 filter 键设置一个回调,将相关的结果集作为参数,并返回它进行过滤,例如,执行一些不能使用rql进行的任意过滤。
pv_section = uicfg.primaryview_section
# in `CWUser` primary view, display `created_by`
# relations in relations section
pv_section.tag_object_of(('*', 'created_by', 'CWUser'), 'relations')
# display this relation as a list, sets the label,
# limit the number of results and filters on comments
def filter_comment(rset):
return rset.filtered_rset(lambda x: x.e_schema == 'Comment')
pv_ctrl = uicfg.primaryview_display_ctrl
pv_ctrl.tag_object_of(('*', 'created_by', 'CWUser'),
{'vid': 'list', 'label': _('latest comment(s):'),
'limit': True,
'filter': filter_comment})
警告
与 primaryview_display_ctrl
分别忽略关系的主客体 tag_object_of
或 tag_subject_of
.为避免在执行期间出现警告,应将它们设置为 '*'
.