writing one2many and many2many fields

Asked by Manu

Hello,

I have been trying most OERPLib and I'm happy with what it does except in writing one2many and many2many fields.

If you read an object, you have nicely mapped one2many and many2many fields to a generator of browse_record instances. However, if you write an object you have to use the awkward openerp syntax. It would be great if you let us to use browse_record instances or ids like you're doing in the many2one fields. As the usual stuff to do in one2many and many2many fields is (6, 0,[ids]), you can make that the default behaviour. If the user wants to do something more obscure with the relationship he can use the usual openerp syntax.

A little test of what I'm saying is:

# helpers
def new_object(obj_type, args):
    object_id = oerp.create(obj_type, args)
    return oerp.browse(obj_type, object_id)

def map2many(browse_objs):
    return [(6, 0, [x.id for x in browse_objs])]

# create a partner with its one2many and many2many records
partner = new_object('res.partner', {'name': 'Manu OERPLib tests', 'lang': 'es_ES'})
print partner.name
partner.ref = "12345678"
addresses = []
addresses.append(new_object('res.partner.address', {'name': 'Addr1', 'street': 'street 1'}))
addresses.append(new_object('res.partner.address', {'name': 'Addr2', 'street': 'street 2'}))
partner.address = map2many(addresses)

categories = []
categories.append(new_object('res.partner.category', {'name': 'Cat1'}))
categories.append(new_object('res.partner.category', {'name': 'Cat2'}))
partner.category_id = map2many(categories)

oerp.write_record(partner)

# update many2many record to use only one category
partner = oerp.browse('res.partner', partner.id)
cat1 = oerp.browse('res.partner.category', categories[0].id)
partner.category_id = map2many([cat1])

oerp.write_record(partner)

Please, think about it,
Manu.

Question information

Language:
English Edit question
Status:
Solved
For:
OERPLib Edit question
Assignee:
No assignee Edit question
Solved by:
Manu
Solved:
Last query:
Last reply:
Revision history for this message
Sébastien Alix (sebastien-alix) said :
#1

Hi Manu,

Indeed, it could be done as you suggest. I have open a bug here: https://bugs.launchpad.net/oerplib/+bug/1179313
I will try to do that for the next release if I can.

Regards,
Seb

Revision history for this message
Manu (manu-tiedra) said :
#2

Excellent news Seb!

Thank you very much!

Revision history for this message
Paulius Sladkevičius @ hbee (komsas) said :
#3

On note from my side, there is and 4,IDS and other possibilities, so I don't suggest to use 6,0,IDS by default, here better to introduce types (unlink, add, update, replace and etc.) which can be mapped to right awkward openerp syntax.

Revision history for this message
Sébastien Alix (sebastien-alix) said :
#4

Hi,
No problem, the current behaviour will be unchanged.
What do you think of that:
- my_record.many_ids = [(4, IDS), ...] # As usual, OERP tuples are detected here
- my_record.many_ids = [IDS or records] # Converted to [(6, 0, IDS)]
- my_record.many_ids += [IDS or records] # [(4, ID) for ID in IDS]
- my_record.many_ids -= [IDS or records] # [(3, ID) for ID in IDS]

Update, create and so on can be ignored I think, as we can use standard OSV methods to achieve the wished operation.

Revision history for this message
Manu (manu-tiedra) said :
#5

Sébastien,

I think that will be awesome. Thank you!

Revision history for this message
Sébastien Alix (sebastien-alix) said :
#6

Hi,

I have pushed the last commit which implement that. If you want to test the trunk branch, you are welcome.
I will try to release the 0.8 before OpenERP Open Days.

Regards,

Revision history for this message
Paulius Sladkevičius @ hbee (komsas) said :
#7

Great! I'll try it, seems there will be need to create scripts in these days.

BTW, I hope there will be chance to meet in open days.

Revision history for this message
Sébastien Alix (sebastien-alix) said :
#8

Of course, I will be there during the 3 days!