Access schema from content object

22 views
Skip to first unread message

Jim Baack

unread,
Dec 9, 2009, 12:53:49 AM12/9/09
to Dexterity development
Want to understand the "recommended" way to access the schema from a
content object. In this code

.......
from zope.app.content import queryContentType
from plone.dexterity.schema import SCHEMA_CACHE
.........
class custom(plone.dexterity.content.Item):
.......
schema1 = queryContentType(self)
schema2 = SCHEMA_CACHE.get(self.portal_type)



the first way always returns None while the second works fine. I have
a schema defined with an XML model via plone.supermodel and as you can
see, a custom content type. Adding, editing and viewing the type works
fine. The documentation led me to believe the first way should work.
Any thoughts? Thanks.

Jim

Martin Aspeli

unread,
Dec 9, 2009, 1:00:33 AM12/9/09
to dexterity-...@googlegroups.com
2009/12/9 Jim Baack <jba...@accelerantsys.com>:
> Want to understand the "recommended" way to access the schema from a
> content object. In this code
>
> .......
> from zope.app.content import queryContentType
> from plone.dexterity.schema import SCHEMA_CACHE

Definitely not by accessing the SCHEMA_CACHE, which is an implementation detail.

> .........
> class custom(plone.dexterity.content.Item):
> .......
>    schema1 = queryContentType(self)
>    schema2 = SCHEMA_CACHE.get(self.portal_type)
>
>
>
> the first way always returns None while the second works fine.

queryContentType should work, and is the recommended way. In which
context are you calling it? What is the schema interface? How did you
register it?

The "other" way is to do lookupSchema() on the FTI.

Do not use the SCHEMA_CACHE.

> I have
> a schema defined with an XML model via plone.supermodel and as you can
> see, a custom content type. Adding, editing and viewing the type works
> fine. The documentation led me to believe the first way should work.

If the schema is the default and wired up correctly, it should be
marked with IContentType and so should work with queryContentType().
If you can give us a bit more detail so that we can reproduce the
issue, then it may be a bug that needs fixing.

Otherwise, getUtility(IDexterityFTI, name=u"my.type").lookupSchema()
will work too, and has the benefit of working even when you don't have
a type object.

Martin

Jim Baack

unread,
Dec 9, 2009, 1:25:32 AM12/9/09
to Dexterity development
Thanks, Martin.

The schema is defined as

class ICustom(plone.directives.form.Schema):
form.model("models/custom.xml")

The content type includes ...
grok.implements(ICustom)

and custom.xml has

<?xml version="1.0" ?>
<model xmlns="http://namespaces.plone.org/supermodel/schema"
xmlns:form="http://namespaces.plone.org/supermodel/form">
<schema>
<field name="status" type="zope.schema.Choice">
<title>Status</title>
<description>Status</description>
<required>True</required>
<default>Active</default>
<vocabulary>custom_vocab</vocabulary>
</field>
</schema>
</model>


I tried the third way you suggested and it worked fine too - clearly
better than mine. I will use that for now. Thanks.

Jim

Martin Aspeli

unread,
Dec 9, 2009, 1:31:58 AM12/9/09
to dexterity-...@googlegroups.com
2009/12/9 Jim Baack <jba...@accelerantsys.com>:

> The schema is defined as
>
> class ICustom(plone.directives.form.Schema):
>    form.model("models/custom.xml")

What is the value of

IContentType.providedBy(ICustom) ?

If it's False, can you raise a bug for this on code.google.com/p/dexterity?

Martin

Jim Baack

unread,
Dec 9, 2009, 1:46:40 AM12/9/09
to Dexterity development
It returned False - I just entered the bug.

Jim
Reply all
Reply to author
Forward
0 new messages