[Zope3-Users] Cloning interfaces/interface fields - how?

Marius Gedminas mgedmin at b4net.lt
Tue Feb 19 14:13:59 EST 2008


On Tue, Feb 19, 2008 at 04:46:29PM +0100, Hermann Himmelbauer wrote:
> I'm over and over confronted with a simple pattern, where I need to clone 
> interface field. For instance, I have the content object, which has an 
> interface, e.g.:
> 
> class IPerson(Interface):
>   name = TextLine()
>   address = TextLine()
> 
> Then, I have for instance a search form, which consists of fields of other 
> interfaces, therefore I tried this:
> 
> class IPersonSearch(Interface):
>   name = IPerson['name']
> 
> The above interface has the problem that the field is referenced, thus 
> changing attributes (e.g. name.required=False) changes also the attributes of 
> the original field.

Don't do that then.

> Therefore I now copy the field, which seems to work (but I'm not 100% 
> certain):
> 
> class IPersonSearch(Interface):
>   name = copy.deepcopy(IPerson['name'])

I would not recommend this.

> Another idea is to entirley skip the IPersonSearch interface and create the 
> interface "on the fly", like this:
> 
> class SearchFrom(z3c.form.EditForm):
>    fields = z3c.form.field.Fields(copy.deepcopy(IPerson).select('name'))

Strike out the deepcopy and you've got the correct solution:

  class SearchFrom(z3c.form.EditForm):
     fields = z3c.form.field.Fields(IPerson).select('name')

> However, this does not seem to work as still some things are referenced 
> instead of copied, so copy.deepcopy seems to fail on interfaces.

What are you trying to achieve?

Zope 3 schema fields ought to be treated as immutable objects.

> What's your opinion on this, how do you apply this pattern?

We don't modify fields, ever.  We use the power of zope.formlib (or
z3c.form) to get dynamic forms.

Marius Gedminas
-- 
NT 5.0 is the last nail in the Unix coffin. Interestingly, Unix isn't in the
coffin... It's wondering what the heck is sealing itself into a wooden box 6
feet underground...
                -- Jason McMullan
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://mail.zope.org/pipermail/zope3-users/attachments/20080219/36fd0914/attachment.bin


More information about the Zope3-users mailing list