It is currently Mon May 21, 2012 7:17 pm

All times are UTC + 2 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: change value of a field for all records
PostPosted: Wed Jul 13, 2011 10:31 am 
Offline

Joined: Wed Jun 08, 2011 10:58 am
Posts: 6
hi,
i want to make an action that will change the receivable account for all customers
I tried to make an action, but i am struggling with the syntax.
That's how i tried to do it, but it does nothing. Can anyone correct the code?

class receivable_wizard(osv.osv_memory):
""" Update partners account receivable """

_name = "sp_partner.receivable.wizard"
_description = "Update partners account receivable"

def update_receivable(self, cr, uid, ids, context):
wizard = self.browse(cr, uid, ids[0], context=context)
partner_pool = self.pool.get('res.partner')
res = {}
for partner in partner_pool.browse(cr, uid, ids, context=context):
res.update({'property_account_receivable': wizard.receivable_id.id})
return res

_columns = {
'receivable_id': fields.many2one('account.account','Receivable',required=True),
}
receivable_wizard()


Top
 Profile  
 
 Post subject: Re: change value of a field for all records
PostPosted: Wed Jul 13, 2011 11:38 am 
Offline

Joined: Wed Jun 08, 2011 10:58 am
Posts: 6
i found the mistake, it works like this:

class receivable_wizard(osv.osv_memory):
""" Update partners account receivable """

_name = "sp_partner.receivable.wizard"
_description = "Update partners account receivable"

def update_receivable(self, cr, uid, ids, context):
wizard = self.browse(cr, uid, ids[0], context=context)
partner_ids = self.pool.get('res.partner').search(cr, uid, [('active','=',True)], context=context)
partner_pool = self.pool.get('res.partner')
res = {}

for partner in partner_pool.browse(cr, uid, partner_ids, context=context):
partner_pool.write(cr, uid, [partner.id], {'property_account_receivable': wizard.receivable_id.id})
return res

_columns = {
'receivable_id': fields.many2one('account.account','Receivable',required=True),
}
receivable_wizard()


Top
 Profile  
 
 Post subject: Re: change value of a field for all records
PostPosted: Wed Jul 13, 2011 6:38 pm 
Offline

Joined: Wed Jan 27, 2010 6:21 pm
Posts: 797
Location: Auckland, NZ
ita wrote:
for partner in partner_pool.browse(cr, uid, partner_ids, context=context):
partner_pool.write(cr, uid, [partner.id], {'property_account_receivable': wizard.receivable_id.id})
return res


This code is pretty inefficient and pointless. Just ditch the loop and instead of making n database writes you make 1.

Code:
partner_pool.write(cr, uid, partner_ids, {'property_account_receivable': wizard.receivable_id.id})

_________________
Graeme


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC + 2 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:

Protected by Anti-Spam ACP