[Zope-dev] Next Step to Bug Resolution???

Carsten Senger senger at rehfisch.de
Fri Jan 16 15:55:42 EST 2009


Hi Tim,

Tim Cook schrieb:
> Thanks for all the assistance.
> 
> On Fri, 2009-01-16 at 18:05 +0100, Martijn Faassen wrote:
> 
>> Yes, you do create new schema fields by subclassing from Field.
>>
>> It's just that we saw you putting a field not in a schema but in what
>> looked like a concrete object. 
> 
> This has given me a BIG pause while I'm working on a simpler example.
> It may actually solve the problem.  
> 
> 
> Are you saying that in order to create a Field that can be used as an
> attribute of another class; I should define it in an interface and ONLY
> in an interface?
> 
> Such like pseudo:
> 
> import Field
> class IAbc(Interface)
> 
> myNewField = Field(
>  ........
> 
> and then when I need to use it in a class, simply state that that class
> implements(IAbc)?

that's what Dan Korostelev said early in the thread: Use the Field only 
in a schema definition. You define your schema in an interface class. 
The interface class describes the interface (fields, attributes, 
methods) that a class implements.

from zope.interface import Interface, implements
from zope.schema import TextLine

class IMyTitleSchema(Interface):

     title = TextLine(...)

class MyTitleClass(object or some other baseclass):

     implements(IMyTitleSchema)

     title = u""

The interface tells other components, e.g. forms: An object instanciated 
from MyTitleClass has an attribute 'title', and title is a TextLine. So 
a form can render the correct widget. Other components do other things 
with this information, like validation when the title attribute is written.

You never use a schema field in something other than an interface class. 
   Don't do:

class MyTitleClass(...):

     ...

     title = TextLine(...)


obj = MyTitleClass()

Sure you can have specialized fields that subclass from Field, TextLine, 
or another base class. E.g. RegistrationNumber(TextLine) that takes care 
to validate the input for a special format. But you use them in an 
interface class, not the class that implements the interface.


..Carsten



More information about the Zope-Dev mailing list