2013-03-03 123 views
1

我正在關注操作敏捷內容對象的a tutorial。它解釋瞭如何創建對象。嘗試創建新的敏捷對象時ComponentLookupError

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

您需要使用您在該Dexterity FTI registration使用相同的名稱。

您可以驗證在portal_types工具註冊什麼名字:

from Products.CMFCore.utils import getToolByName 

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

或訪問在ZMI的portal_types工具在瀏覽器中,並期待在類型有列表;它們在工具中被列爲typeid (Type Title)。如果您的類型沒有列在那裏,請確保您先正確註冊了您的類型。

請注意,爲此,您需要正確設置本地組件管理器。通常,這會自動發生,但如果您使用的是bin/instance run腳本或使用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) 
+0

內容類型'degu.Product'已列出,但是我得到時同樣的錯誤試圖使用它。另外,當使用'bin/instance debug'時,context沒有定義,所以我使用app.Plone來獲得類型。 – 2013-03-03 12:41:09

+0

@DanielHernández:啊,使用'bin/instance debug'不會設置本地組件管理器。這是一個*不同的問題。 – 2013-03-03 12:41:58

+0

我有同樣的問題惠特'bin /實例運行'。 – 2013-03-03 12:58:35