[Zope3-Users] Re: How do I get the schema/field list from an instance?

Philipp von Weitershausen philipp at weitershausen.de
Wed Feb 14 13:22:53 EST 2007


Vinny wrote:
> In a ZPT I would do something like:
> 
> for field in fieldsInObject(row) # which has (ordered?) fields
>   th cell: field.name
> for row in rowView
>   for field in fieldsInObject(row) # which has (ordered?) fields
>     td cell: field.value
> 
> I know the above is broken in several ways.  I've been
> trying various methods all evening.  The closest I get
> is the __dict__ usage.  Ideally, I would get the 
> interface provided by the object and feed that to 
> form.Fields() but I can't determine how to get an
> interface value that can be fed to form.Fields().
> 
> iface = zope.interface.providedBy(obj)
> 
> doesn't seem to work.

By itself it won't work. You'll have to do a bit more. First of all, an 
object can provide 0, 1 or more interfaces. Second, schemas are just 
interfaces whose specifications are fields (a schema can also have 
method specifications, there's no restriction to "mixing" a traditional 
interface with a schema). Therefore:

   for iface in zope.interface.providedBy(obj):
       for name in iface:
           field = iface[name]
           if zope.schema.interfaces.IField.providedBy(field):
               # you've got a schema field, now you can work with
               # field.title, field.description, etc. (see IField)

-- 
http://worldcookery.com -- Professional Zope documentation and training
Next Zope 3 training at Camp5: http://trizpug.org/boot-camp/camp5



More information about the Zope3-users mailing list