[Zope-CMF] how to store a reference?

Mark McEahern mark@mceahern.com
Wed, 23 Oct 2002 17:24:48 -0500


[Doug Hellmann [mailto:doug@hellfly.net]]
> You probably want to just store the name, and convert to the real
> object when you try to use it another script.  Otherwise you're
> going to end up with multiple copies of the Contact objects.
>
> As far as the actual conversion, how about a Catalog lookup?

This whole thing seems like something that I shouldn't have to jump through
a lot of hoops to do.  Display a list of objects to relate to another object
in a form.  Pre-select the current ones.  And then, on POSTing of the form,
update the containing object's list.

What I ended up doing is the following ugly hack inside the edit method of
PressRelease (the containing object)--my questions are all embedded in
comments...

        # contact_ids gets passed from press_release_edit_form.pt to
        # press_release_edit.py to PressRelease.edit():

        if contact_ids is not None:
            # Whether one or more was selected in the multi-select,
            # I want a list.
            contact_ids = utils.make_list(contact_ids)
            # FIXME: This seems fragile--just having all these details
            # inside PressRelease...
            available_contacts = self.aq_parent.objectValues(['Contact'])
            # Is the following an alternative to the above line?
            # available_contacts =
self.aq_parent.portal_catalog(portal_type='Contact')
            # Would that limit it to Contact objects in this object's
container?
            dict_of_contacts = {}
            for c in available_contacts:
                dict_of_contacts[c.getId()] = c
            store_contacts = []
            for contact_id in contact_ids:
                # This will raise an error if the contact_id doesn't exist.
                store_contacts.append(dict_of_contacts[contact_id])
            self.contacts = store_contacts

I didn't like the idea of storing the ids only.  That just pushed the
problem somewhere else.

Again, this seems like a framework service--am I missing something?

Thanks,

// mark

-