Rick Hurst Web Developer in Bristol, UK


Plone bulk update of default values for new attribute

Scenario: I added a new attribute “orderPriority” to a few different archetypes based content types. These attributes had default values. As this is an existing site, there were 400 or so existing objects that had not picked up these default values – i.e. they would only pick up the value after they had been re-saved. To make sure these objects picked up the default value I created a skin script, which I proxied as manager (watch out for indentation here, really must find a better way to display preformatted code):-

catalog = context.portal_catalog
# objects are all in folder /directory/listing/
listing_folder = getattr(context.directory, 'listing')

for brain in catalog(portal_type = ['NameOfMyContentType']):
print brain.portal_type + ' ' + brain.id + ' ' + brain.Title

listing = getattr(listing_folder, brain.id)
#this gets the default vakue from the schema
order = listing.getOrderPriority() #this sets it

return printed

I needed to be able to sort by this field in a catalog query/ Topic/SmartFolder/Collection so, so I then added the field as a fielindex in the portal catalog, reindexed the catalog (ZMI) and used the collection edit options in site setup to add it as a field

archived comments

What should also work is to go to the ZMI -> archetype_tool -> Update Schema tab and there update the schema of your content type (select ‘all objects’ to be sure to get them all; and check the ‘remove schema attribute’ box when it is available).

Maurits van Rees 2008-09-17 21:49:15

excellent, thanks for that – i’ll try that next time 🙂

Rick 2008-09-18 07:51:50