1

我正在关注有关操作灵巧内容对象的教程。它解释了如何创建对象。

from zope.component import createObject
context = createObject('example.type')

但我不知道该放什么example.type。我尝试使用IProduct,degu.product.IProductdegu.Product. 但是它们都引发了 ComponentLookupError。

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/daniel/.buildout/eggs/zope.component-3.9.5-py2.6.egg/zope/component/_api.py", line 220, in createObject
    return getUtility(IFactory, __factory_name, context)(*args, **kwargs)
  File "/home/daniel/.buildout/eggs/zope.component-3.9.5-py2.6.egg/zope/component/_api.py", line 169, in getUtility
    raise ComponentLookupError(interface, name)
ComponentLookupError: (<InterfaceClass zope.component.interfaces.IFactory>, 'degu.commerce.product.IProduct')
4

1 回答 1

4

您需要使用在Dexterity FTI 注册中使用的相同名称。

您可以验证在该portal_types工具中注册了哪些名称:

from Products.CMFCore.utils import getToolByName

typestool = getToolByName(context, 'portal_types')
print typestool.listContentTypes()

portal_types在浏览器中访问 ZMI 中的工具并查看那里的类型列表;它们typeid (Type Title)在工具中列出。如果您的类型未在此处列出,请确保您首先正确注册了您的类型。

请注意,要使其正常工作,您需要正确设置本地组件管理器。通常,这会自动发生,但如果您使用的是bin/instance runscript 或 use bin/instance debug,则不会发生。在这种情况下,您需要手动执行此操作:

from zope.app.component.hooks import setSite
from Testing.makerequest import makerequest

app = makerequest(app)
site = app[site_id]
setSite(site)

您可能还想设置一个当前用户:

from AccessControl.SecurityManagement import newSecurityManager

user = app.acl_users.getUser('admin').__of__(site.acl_users)
newSecurityManager(None, user)
于 2013-03-03T11:41:16.463 回答