Archetypes API提供了以编程方式填充初始值的default_method()。
但是,由于这是一个类方法,所以它不适用于archetypes.schemaextender。与扩展器等效的方法是什么?
发布于 2012-05-11 13:18:16
如果既没有field.default也没有field.default_method,您可以使用IFieldDefaultProvider适配器。请参阅Archetypes.Field.Field类getDefault方法中的以下代码片段:
if not self.default:
default_adapter = component.queryAdapter(instance, IFieldDefaultProvider, name=self.__name__)
if default_adapter is not None:
return default_adapter()还有IFieldDefaultProvider:
class IFieldDefaultProvider(Interface):
"""Register a named adapter for your content type providing
this interface, with a name that is equal to the name of a
field. If no default or default_method is set on that field
explicitly, Archetypes will find and call this adapter.
"""
def __call__():
"""Get the default value.发布于 2014-10-13 18:52:39
这是使用Mixin类处理archetypes.schemaextender时default_method()的解决方案。字段初始值的代码应该在这样的mixin类中的名为'getDefault‘的方法中,您可以将其放在扩展字段的声明之前:
class ProvideDefaultValue:
""" Mixin class to populate an extention field programmatically """
def getDefault(self, instance):
""" Getting value from somewhere (in this ex. from same field of the parent) """
parent = aq_parent(instance)
if hasattr(parent, 'getField'):
parentField = parent.getField(self.__name__)
if parentField is not None:
return parentField.getAccessor(parent)现在,您可以在相应的扩展类声明中包含此方法:
class StringFieldPrefilled(ExtensionField, ProvideDefaultValue, atapi.StringField):
""" Extention string field, with default value prefilled from parent. """注意:您不需要在扩展模式字段定义中添加default_method。
https://stackoverflow.com/questions/10542486
复制相似问题