I'd like to list all the fields (and the values) of a dexterity object.
I can easily access a single field value like this myobject.myfield
But I don't know how to list all the available fields of an object.
Dexterity objects are defined by a schema, which is enumerable:
from zope.interface.interfaces import IMethod
for name, desc in IYourDexteritySchema.namesAndDescriptions():
value = getattr(yourDexterityInstance, name)
if IMethod.providedBy(desc):
# It's a method, call it
value = value()
Here I use the .namesAndDescriptions()
method, but you can also just use the interface as an iterator if you don't have any methods on your schema:
for name in IYourDexteritySchema:
value = getattr(yourDexterityInstance, name)
Last but not least, zope.schema
has a utility method named getFieldsInOrder
that will filter on zope.schema
fields in an interface; the above methods also list methods and non-schema attributes, getFieldsInOrder
only lists zope.schema
specific attribute types:
from zope.schema import getFieldsInOrder
for name, field in getFieldsInOrder(IYourDexteritySchema):
value = getattr(yourDexterityInstance, name)
If you defined the schema through-the-web, you may not know how to look up the interface for your type. You can also retrieve the schema interface through the Factory Type Information, or FTI for short:
from plone.dexterity.interfaces import IDexterityFTI
from zope.component import getUtility
schema = getUtility(IDexterityFTI, name='your.dexterity.type').lookupSchema()
where schema
now holds your interface.