'list the fields of a dexterity object

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.



Solution 1:[1]

as David noted, I could not find a way to enumerate the fields coming from behaviors.

So, digging into plone.dexterity, in utils.getAdditionalSchemata function I found this way to roughly solve the problem:

from zope.schema import getFieldsInOrder
from plone.behavior.interfaces import IBehaviorAssignable

behavior_assignable = IBehaviorAssignable(context)
if behavior_assignable:
  behaviors = behavior_assignable.enumerateBehaviors()
  for behavior in behaviors:
    for k,v in getFieldsInOrder(behavior.interface):
      # ...

Solution 2:[2]

I found a way to do it. Since the dexterity scheme varies I could not use Christophs suggestion directly. But this works now

for key, value in getFieldsInOrder(obj.getTypeInfo().lookupSchema()):
    value = getattr(getattr(obj, key), 'output', getattr(obj, key))

Solution 3:[3]

You can alternatively use the FTI:

from plone.dexterity.interfaces import IDexterityFTI
from zope.component import getUtility

fti = getUtility(IDexterityFTI, name='rgd.venues.venue')
schema = fti.lookupSchema()
fields = getFieldsInOrder(schema)
for key, value in fields:
    # so something

Solution 4:[4]

Figure out how to list all fields from fields and behaviors:

from plone.behavior.interfaces import IBehavior
from plone.dexterity.interfaces import IDexterityFTI
from zope.component import getUtility


def get_fields(portal_type):
    fti = getUtility(IDexterityFTI, name=portal_type)
    schema = fti.lookupSchema()
    fields = schema.names()
    for bname in fti.behaviors:
        factory = getUtility(IBehavior, bname)
        behavior = factory.interface
        fields += behavior.names()
    return fields

Solution 5:[5]

You can use iterSchemata and getFieldsInOrder:

from plone.dexterity.utils import iterSchemata
from zope.schema import getFieldsInOrder

for schema in iterSchemata(obj):
    print(u'  %s' % schema)
    for field in getFieldsInOrder(schema):
        print(u'    %s\t%s' % field)

if obj is a Document it outputs

plone.dexterity.schema.generated.Plone_5_1643036771_2_583572_0_Document
plone.app.dexterity.behaviors.discussion.IAllowDiscussion
  allow_discussion    plone.app.dexterity.behaviors.discussion.IAllowDiscussion.allow_discussion
plone.app.dexterity.behaviors.exclfromnav.IExcludeFromNavigation
  exclude_from_nav    plone.app.dexterity.behaviors.exclfromnav.IExcludeFromNavigation.exclude_from_nav
plone.app.dexterity.behaviors.id.IShortName
  id  plone.app.dexterity.behaviors.id.IShortName.id
plone.app.dexterity.behaviors.metadata.IDublinCore
  title       plone.app.dexterity.behaviors.metadata.IBasic.title
  description plone.app.dexterity.behaviors.metadata.IBasic.description
  subjects    plone.app.dexterity.behaviors.metadata.ICategorization.subjects
  language    plone.app.dexterity.behaviors.metadata.ICategorization.language
  effective   plone.app.dexterity.behaviors.metadata.IPublication.effective
  expires     plone.app.dexterity.behaviors.metadata.IPublication.expires
  creators    plone.app.dexterity.behaviors.metadata.IOwnership.creators
  contributors        plone.app.dexterity.behaviors.metadata.IOwnership.contributors
  rights      plone.app.dexterity.behaviors.metadata.IOwnership.rights
plone.app.contenttypes.behaviors.richtext.IRichTextBehavior
  text        plone.app.contenttypes.behaviors.richtext.IRichTextBehavior.text
plone.app.relationfield.behavior.IRelatedItems
  relatedItems        plone.app.relationfield.behavior.IRelatedItems.relatedItems
plone.app.versioningbehavior.behaviors.IVersionable
  changeNote  plone.app.versioningbehavior.behaviors.IVersionable.changeNote
  versioning_enabled  plone.app.versioningbehavior.behaviors.IVersionable.versioning_enabled
plone.app.contenttypes.behaviors.tableofcontents.ITableOfContents
  table_of_contents   plone.app.contenttypes.behaviors.tableofcontents.ITableOfContents.table_of_contents

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Maurizio Delmonte
Solution 2 domruf
Solution 3 Christoph Böhner
Solution 4
Solution 5