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

Hermann Himmelbauer dusty at qwer.tk
Tue Feb 19 10:46:29 EST 2008


Hi,
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.

Therefore I now copy the field, which seems to work (but I'm not 100% 
certain):

class IPersonSearch(Interface):
  name = copy.deepcopy(IPerson['name'])

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

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's your opinion on this, how do you apply this pattern?

Best Regards,
Hermann

-- 
hermann at qwer.tk
GPG key ID: 299893C7 (on keyservers)
FP: 0124 2584 8809 EF2A DBF9  4902 64B4 D16B 2998 93C7


More information about the Zope3-users mailing list