How to use 'createObject'?

16 views
Skip to first unread message

Julian Sanchez

unread,
Sep 13, 2017, 1:20:55 PM9/13/17
to pylons-discuss
After reading how to use the zca page I've managed to register a few utilities and adapters through
request.registry.registerUtility(...)
and
request.registry.getUtility(...)

How can you hook up the zope.component.createObject method?  I did my
request.registry.registrerUtility(foo_factory, IFactory, 'Foo')

but of course creatObject by itself will go against the global repository (and of course I get the dreaded 'could not adapt' error)

I tried the trick:
mycontext = zope.configuration.config.ConfigurationMachine()
mycontext
.registry = request.registry


..and then
myfoo = createObject('Foo', context=mycontext)

But that also returns 'could not adapt'

Is my only option to do a
request.registry.getUtility(IFactory, 'Foo')()

instead of createObject?

Thanks,
Julian

Michael Merickel

unread,
Sep 13, 2017, 2:26:51 PM9/13/17
to Pylons
I know almost nothing about what you just asked but I will point you to https://docs.pylonsproject.org/projects/pyramid/en/1.9-branch/narr/zca.html#using-the-zca-global-api-in-a-pyramid-application as well as the `config.hook_zca` method.

- Michael

--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discuss+unsubscribe@googlegroups.com.
To post to this group, send email to pylons-discuss@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/46ff94d2-ef9e-4741-9d64-ae8e9e11cf8f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Julian Sanchez

unread,
Sep 13, 2017, 6:13:07 PM9/13/17
to pylons-discuss
Thanks Michael.  That's the page that I had read.  Off the 3 options the one with the least possible side effects appears to be the one where you simply use the registry that pyramid generates and includes it to the request, which is what I'm pursuing at the moment.  

Does anybody know if there would be any side-effects if I had a config.hook_zca() statement in a 'includeme(config)' method if my pyramid app were to be used by another pyramid app?

Julian



On Wednesday, September 13, 2017 at 1:26:51 PM UTC-5, Michael Merickel wrote:
I know almost nothing about what you just asked but I will point you to https://docs.pylonsproject.org/projects/pyramid/en/1.9-branch/narr/zca.html#using-the-zca-global-api-in-a-pyramid-application as well as the `config.hook_zca` method.

- Michael
On Wed, Sep 13, 2017 at 12:20 PM, Julian Sanchez <forei...@gmail.com> wrote:
After reading how to use the zca page I've managed to register a few utilities and adapters through
request.registry.registerUtility(...)
and
request.registry.getUtility(...)

How can you hook up the zope.component.createObject method?  I did my
request.registry.registrerUtility(foo_factory, IFactory, 'Foo')

but of course creatObject by itself will go against the global repository (and of course I get the dreaded 'could not adapt' error)

I tried the trick:
mycontext = zope.configuration.config.ConfigurationMachine()
mycontext
.registry = request.registry


..and then
myfoo = createObject('Foo', context=mycontext)

But that also returns 'could not adapt'

Is my only option to do a
request.registry.getUtility(IFactory, 'Foo')()

instead of createObject?

Thanks,
Julian

--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discus...@googlegroups.com.
To post to this group, send email to pylons-...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages