[Zope-dev] [BlueBream] Referring to same interface using zope.schema.Object

Jacob Holm jh at improva.dk
Fri Jul 22 08:53:29 EDT 2011


On 2011-07-22 14:32, Joshua Immanuel wrote:
> Hello,
> 
> On Fri, 2011-07-22 at 13:41 +0200, Jacob Holm wrote:
>> On 2011-07-22 13:26, Brian Sutherland wrote:
>>> This would be my first guess:
>>>
>>>     class INode(Interface):
>>>         pass
>>>      
>>>     INode.parent = Object(
>>>             title=u"Parent node",
>>>             schema=INode
>>>             )
>>>
>>>     INode.children = List(
>>>             title=u'Child nodes',
>>>             value_type=Object(schema=INode)
>>>             )
>>>
>>
> 
> The method suggested by Brian works without any issues. :)
> 

It "works" only in the the sense that it doesn't throw an exception.
It does not define the desired schema.


>>
>> And that guess would be wrong.  You can't add fields to an existing
>> schema like that (not sure if you can in other ways).  You *can*
>> change
>> an existing field however, so a working solution would be:
>>
>>     class INode(Interface):
>>
>>         parent = Object(
>>             title = u'Parent node',
>>             schema = Interface, # set to INode later
>>             )
>>
>>         children = List(
>>             title = u'Child nodes',
>>             value_type = Object(schema=Interface), 
>>             )
>>
>>     INode['parent'].schema = INode
>>     INode['children'].value_type.schema = INode 
> 
> I thought this also should work without any issues. 

Trust me, it does.


> But when I ran the
> debug shell to list out the attributes of INode using dir(INode) I
> couldn't find the 'parent' and 'children' attributes in it.

The fields of the schema are not *supposed* to show up in "dir()".
Despite the use of the "class" statement to define them, interfaces are
*not* classes.  They are instances of
zope.interface.interface.InterfaceClass, and act like (read-only)
containers of their fields.

Try using 'list(Inode)' for testing and you will see that "my" method
works as expected.


> Even worse
> part is, if there is another field say
> 
>                 name = TextLine(title=u'Node name')
>                 
> This 'name' attribute is also is not visible along with 'parent' and
> 'children' attributes.
> 
> Can someone explain why is this so?
> 

Right.  As explained above, it isn't supposed to.


> (Even though the problem gets solved by Brian's method, just curious to
> know)
> 

I hope to have convinced you by now that it really isn't solved by
Brian's method, but by mine.  (Although I wouldn't call it "my" method
as such.  I'm sure I have taken it from somewhere else)


- Jacob


More information about the Zope-Dev mailing list