Ben
Ben

Reputation: 2472

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?

Upvotes: 2

Views: 114

Answers (1)

Giacomo Spettoli
Giacomo Spettoli

Reputation: 4496

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))

Upvotes: 4

Related Questions