logo

Personal tools
Vasudeva Documentation Archetypes Tips Generating ids with portal_factory
Document Actions

Generating ids with portal_factory

If you are using portal_factory with your content types (and you should), and you need to programmatically generate an object id when it being saved, there is a special mantra you have to utter to make it work correctly.

For example, say we have a content type where the id will be generated from a language code. When you set the id, make sure your code looks like this:

  # generate the new id
  id = self._generateMyNewId()

  # we are in a subtransaction, commit it
  get_transaction().commit(1)

  # now it's okay to set the id
  self.setId(id)

The important line of code is get_transaction().commit(1), which commits the subtransaction that portal_factory creates to edit your object. Without doing that, setId(id) wouldn't propagate outside of the transaction to the real object.

page created by admin last modified 2005-08-25 19:45