简体   繁体   中英

How can a zope.interface.Interface require implementation of a list of objects that implement another interface?

In the following example, I want objects implementing IParentInterface to be required to supply a mycollection attribute that is a list of objects implementing IChildInterface.

from zope.schema import Text, List
from zope.interface import Interface

class IChildInterface(Interface):
    someField = Text()

class IParentInterface(Interface):
    mycollection = List(value_type=IChildInterface)

Is there a straightforward way to do this, or would I need to use invariants?

This should work:

from zope.schema import Text, List, Object
from zope.interface import Interface

class IChildInterface(Interface):
    someField = Text()

class IParentInterface(Interface):
    mycollection = List(value_type=Object(title=u'Child',
                                          schema=IChildInterface))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM