Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

2

how to format value for a many2many field thru XML RPC

By
Nicolas C
on 4/10/13, 7:56 PM 9,622 views

Hello,

Up to now I was able to search/read/create and write objects thru XML RPC (using perl script).

Until now I was only dealing with "simple" values like : string (name of a product) number (price) boolean (true/false) ...

But I need now to deal with "many2many" field and I don't know how to perform...

As seen within "XML RPC perl - OpenERP dev book example" h t t p : / / doc.openerp.com/v6.0/developer/6_22_XML-RPC_web_services/#perl-example I build the "object data" as a hash reference and it work fine for all "simple values".

For "many2many" I tried to use list of "target model id" in the following format within the "object data" hash :

['1', '2']
[['1', '2']]
[('1', '2')]

or directly using the native "array" from perl (ie: @array) But I get any error while writing the data (the write is correctly running for other fields like name) and .... so.. nothing happened for this many2many field :(

I'm currently dealing with "multi variant" product an the field I try to update is "dimension_type_ids" from model "product.template" on v6.1/ubuntu 12.04 using apps extra module "product_variant_multi".

Any idea/help is welcome (even with other language than perl) !!!

Have a nice day.

2

saad

--saad--
1008
| 5 4 8
Casablanca, Morocco
--saad--
saad
On 4/11/13, 4:28 PM

When we are creating a many2many field we use this syntax:

'field_name':fields.many2many('related_model_name','relation_table_name','current_model_id','related_model_id', 'string' )

If we want manually fill this field, we should insert data in relation_table_name by executing this querie:

cr.execute('insert into relation_table_name (current_model_id, related_model_id) values(%s,%s)',(first_value,second_value))

OpenObject (OpenERP Framework) provide a list of tuples to use many2many fields in an easy way. Here is the list of tuple that are accepted, with the corresponding semantics

The dimension_type_ids field is a many2many relationship, and the (6, 0, Ids) means to replace any existing records (of product.variant.dimension.option) with those in Ids, because you're calling a create() method.

Thank you very much !!!!!! Now I understand very much : how OpenERP framework internally work and... what I have to do ! I will test right now and let you know. Your link to the "list of tuple" is really great !!!!

Nicolas C
on 4/11/13, 5:14 PM
4
Nicolas C
On 4/11/13, 2:17 PM

Hello,

After solving some issue on the Ubuntu side, I have now a "/var/log/openerp-server.log" file working again.

Launching OpenERP server with additional options "--debug --log-level=debug_rpc_answer" now provide me useful informations !!!

When performing "product.template" edition thru the web interface, I captured the following syntax :

'dimension_type_ids': [[6, False, [1, 2]]]

I fully know/understand what is the last part of the "value" => "[1, 2]". This is the list of "foreign IDs" from model "product.variant.dimension.option" I just assigned to my template from the Web interface. But I don't know at all what the begin "6, False" refer to... :(

Does anyone have a clue about this syntax ? I'm not familiar with python at all so maybe this is some particular python syntax ? Or an OpenERP "usual format" ?

Any advice will be nice, I keep searching on my side ;o)

Just FYI, when performing a "read" on the same object I only get :

'dimension_type_ids': [1, 2]

Have a nice day everybody.

@++ Nicolas


UPTADE based on S@@D awesome answer

As only one answer is allowed, I'll update my own response here based on S@@D answer.

For an people looking to use perl as XML RPC interface to OpenERP, here is a sample of template.product object data (ie: a perl hash ref):

$object_data = {
    'name' => 'this is my template',
    'uom_id' => '1',
    'uom_po_id' => '1',
    'procure_method' => 'make_to_order',
    'variant_model_name' => '[_o.option_id.code_]',
    'do_not_update_variant' => '1',
    'sale_ok' => '1',
    'dimension_type_ids' => '[[6, 0, [1, 2]]',
    'variant_model_name_separator' => '\'\'',
    'supply_method' => 'produce',
    'type' => 'product',
    'categ_id' => '24',
    'is_multi_variants' => '1'
};

Where :

'dimension_type_ids' => '[[6, 0, [1, 2]]',

Is an update of a "many2many" field.

Have a look to S@@D link about tuple syntax (sorry but I have not enough karma to add link in my own answer). At the bottom of the developer book documentation page, you will have the "write" explained with all kindly syntax for many2many, one2many, many2one (the simpler one ;) ).

Note: I was only aware of CRUD (create/read/update(ie write)/delete(ie unlink) capability of OpenERP XML RPC... I'll now have a look to others listed in the doc page ;)

Thank you again S@@D Have a nice day

@++ Nicolas

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 4/10/13, 7:56 PM
Seen: 9622 times
Last updated: 3/16/15, 8:10 AM