Discussion:
[Erp5-dev] Deleting a list of objects in a container
Guillaume Michon
2004-07-28 09:50:58 UTC
Permalink
Hi all,


The following code does not work as expected :

for o in container.contentValues():
container._delObject(o.getId())

The reason is that python does not calculate the expression
"container.contentValues()" only one time, but at each passage through the
for. So modifying content of container changes the result of the expression.
Then the behavior is undeterminable.

The secured way to do is to build a list of ids to delete, then delete them :

to_delete = []
for o in container.contentValues():
to_delete.append(o.getId())
container.deleteContent(to_delete)


Regards,


Guillaume
Guillaume Michon
2004-07-28 09:57:07 UTC
Permalink
Post by Guillaume Michon
to_delete = []
? ?to_delete.append(o.getId())
? ?container.deleteContent(to_delete)
Of course, it is :
to_delete = []
for o in container.contentValues():
?to_delete.append(o.getId())
container.deleteContent(to_delete)


Sorry

Guillaume
Stefane Fermigier
2004-07-28 10:00:54 UTC
Permalink
Post by Guillaume Michon
Hi all,
container._delObject(o.getId())
The reason is that python does not calculate the expression
"container.contentValues()" only one time, but at each passage through the
for. So modifying content of container changes the result of the expression.
Then the behavior is undeterminable.
to_delete = []
to_delete.append(o.getId())
container.deleteContent(to_delete)
You mean probably:

to_delete = []
for o in container.contentValues():
to_delete.append(o.getId())
container.deleteContent(to_delete)

But I think this would be simpler:

container.deleteContent(container.contentIds())

S.
--
St?fane Fermigier, Tel: +33 (0)6 63 04 12 77 (mobile).
Nuxeo Collaborative Portal Server: http://www.nuxeo.com/cps
Gestion de contenu web / portail collaboratif / groupware / open source!
-------------- next part --------------
A non-text attachment was scrubbed...
Name: sf.vcf
Type: text/x-vcard
Size: 275 bytes
Desc: not available
URL: <http://mail.tiolive.com/pipermail/erp5-dev/attachments/20040728/40075540/attachment.vcf>
Loading...