我们有一个基于 Dexterity 的内容类型,它必须从其父字段继承字段的默认值。我们使用以下内容:
在模型中:
<model xmlns="http://namespaces.plone.org/supermodel/schema"
xmlns:indexer="http://namespaces.plone.org/supermodel/indexer"
xmlns:form="http://namespaces.plone.org/supermodel/form">
<schema>
...
<field name="subjects" type="zope.schema.Tuple" indexer:searchable="true">
...
<defaultFactory>my.package.content.default_subjects</defaultFactory>
...
</field>
</schema>
</model>
工厂声明如下:
from zope.schema.interfaces import IContextAwareDefaultFactory
...
@provider(IContextAwareDefaultFactory)
def default_subjects(context):
return getattr(context, 'subjects', ())
这在运行实例时工作正常:
(Pdb) context
<MyType at /Plone/folder>
(Pdb) type(context)
<type 'Acquisition.ImplicitAcquisitionWrapper'>
但是在运行测试时失败,因为没有包装上下文:
(Pdb) context
<MyType at test>
(Pdb) type(context)
<class 'my.package.content.MyType'>
我该如何解决这个问题?