GetToolByName () and others

What is the recommended method to access Plone persistent utilities and why?

  • getToolByName (context, "portal_url")

  • Direct purchase: context.portal_url

  • ITools Interface

and etc.

+6
source share
2 answers

I think the answer is in the getToolByName code ( http://svn.zope.org/Products.CMFCore/trunk/Products/CMFCore/utils.py?view=markup ). This method does this:

  • as the first attempt, he tries to get the desired tool by looking at the interface with getUtility and including it in the context (which seems good to me)
  • as a backup, he is trying to directly get the tool from the original context.

Thus, the only way to β€œmanage all of them” is as follows: getToolByName But, as @keul said, caching is also involved and see here ( http://collective-docs.readthedocs.org/en/latest/misc/context. html # itools-interface ) that using the ITools interface when it is not interrupted due to tools that do not yet implement this interface is faster.

For the reasons above, in the end I would suggest:

  • ITools (faster)
  • getToolByName (safer)

(since direct receipt has already been verified by getToolByName, if it fails, you will not have more luck)

+5
source

Using ITools utilities should be the best method, as they are cached. However, this sometimes gave me problems (related to the portal_membership tool, but I don’t remember the details), so in rare cases I switch to getToolByName. The latter is a direct acquisition.

+2
source

Source: https://habr.com/ru/post/911065/


All Articles