Dexterity开发手册:第六章 第14节 定制内容类

Dexterity开发手册:第六章 第14节 定制内容类
给Dexterity内容类型添加一个定制的类实现
当我们学习配置 Dexterity FTI时,我们看到klass 属性,并且也看到了怎样用 Container 或者 Item 基类。这两个类定义在 plone.dexterity.content 模块,表现容器类型或非容器类型。对于大多数应用,这两个类将足够了。为另外的功能我们用行为、适配器、事件机制、接口schema来实现。然而 在某些情况下,定制一个类的实现是有用的、也是必须的。创建一个定制类相当简单,从基类生成:
from plone.dexterity.content import Item class MyItem(Item): """A custom content class""" ...为容器类型,我们做:
from plone.dexterity.content import Container class MyContainer(Container): """A custom content class""" ...你能添加任意的属性和方法到这个类,为使用该类,在FTI中设置klass属性:<property name="klass">my.package.myitem.MyItem</property>这将引起标准的Dexterity factory工厂在用户提交添加表单时实例化该类。
As an alternative to setting klass in the FTI, you amy provide your own IFactory utility for this type in lieu of Dexterity's default factory (see plone.dexterity.factory). However, you need to be careful that this factory performs all necessary initialisation, so it is normally better to use the standard factory.
定制类的提示:
  • 肯定选择正确的基类:或者 plone.dexterity.content.Item 或者 plone.dexterity.content.Container.
  • 如果要混合其他基类,安全的做法是将 ItemContainer 类放在前面。
  • 如果定制类的构造器,肯定它能被不带参数调用,或者用一个可选的id参数来给定名称。

设置