logo

Personal tools
Vasudeva Documentation Archetypes Tips The magical post-create script
Document Actions

The magical post-create script

If you are making content types that are anything more than trivial, you frequently will want to do some computations and manipulations of the content fields after they are edited by the user.

Using mutators sucks, because they get called **every** time the field's value is set, which is a lot. What you really want is one method that will be called **only** when the user clicks Save on the edit form, and **not** when the object is created or modified programmatically.

It turns there is such a thing. To set it up, you need to define a method in your content class that will be called, and then point AT to your method by putting one line of code **after** the method definition, like this:

  def postCreateScript(self):
"""Callback for post-editing"""

self._doSomeComputations()
self._setGeneratedId()

at_post_create_script = postCreateScript

Don't be fooled by the "create" in at_post_create_script. Your method will be called after every edit, whether or not the object was just created. Best of all, it is called once after all validation has passed, so you know that the object fields are valid at that point.

NOTE: If you have set _at_rename_after_creation = True in your content class, the post-create script will be called **before** auto-renaming occurs.

It's clean, it's fun, and it works...at least in the latest version of plone/AT.

page created by admin last modified 2006-04-20 17:00

Create and edit

Posted by viliam at 2006-04-20 17:06
In recent version of archetypes the at_post_create_script() method gets called only at object creation as the name suggests. However, there is also at_post_edit_script() method that is called - surprise - after every edit.