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

Vinny vinny-mail-01+zope3users at palaceofretention.ca
Tue Feb 13 23:40:33 EST 2007


Hi,

Zope 3.3.0

I hope everyone is well today.  

If I have a container how do I find out what types of objects
it contains and then from that, how do I find out the schema
of the contained object?

Eg. I have a SQLAlchemyContainer with a SQLAlchemyContainerView.
The container contains the mapped class for a sqlalchemy 
table.  

Here's the view code that I've been struggling with:

class SQLAlchemyContainerView(BrowserView):

    def __init__(self, context, request):
        self.context = context
        self.request = request

    def fieldsInObject(self):
        objlist = [obj for key, obj in self.context.items()]
        obj = removeSecurityProxy(objlist[0])
        return obj.__dict__

    def rowView(self):
        for obj in self.context.values():
            yield obj

My goal is to generalize a view that creates an HTML table for
an arbitrary database table.  I want to register it for
the ISQLAlchemyContainer interface.  So I need to know the
objects contained and their schema.  

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.

Anyone done this already or know of some (comprehensible, 
for a noob) documentation/source code for this?

Thanks in advance.
Vinny


======== some code =========

class IISO6392Language(Interface):
    """The interface definition of the iso639_2_language table.
    """

    id = Int(
             title=_(u"serial id"),
             description=_(u"primary key is derived from this content"),
             readonly=True,
             required=True,
             )

    lookup = TextLine(
             title=_(u"Lookup code.  Message Id"),
             description=_(u"This may be used as a message id in i18n terms.  Should be unique in a table."),
             required=True,
             max_length=1024
             )


class ISO6392Language(object):
    """The implementation of IISO6392Language.  Hopefully, the
    FieldProperty usage will help validate user data."""

    implements(IISO6392Language)

    id = FieldProperty(IISO6392Language['id'])
    lookup = FieldProperty(IISO6392Language['lookup'])


iso639_2_language = sqlalchemy.Table(
     'iso639_2_language',
     z3c.zalchemy.metadata,
     sqlalchemy.Column('id', sqlalchemy.Integer, primary_key=True),
     sqlalchemy.Column('lookup', sqlalchemy.String(1024), nullable=False, default=u''),
     )

ISO6392Language.mapper = sqlalchemy.mapper(ISO6392Language, iso639_2_language)

==============





More information about the Zope3-users mailing list