跳至內容
選單
此問題已被標幟
4 回覆
27906 瀏覽次數

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.

頭像
捨棄
最佳答案

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 !!!!

作者 最佳答案

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

頭像
捨棄
最佳答案

Thank you Nicolas C, i was trying to add some records to a Many2many field in Python.

model_obj.write({'field': [[6, 0, ids]]})
頭像
捨棄
相關帖文 回覆 瀏覽次數 活動
4
10月 21
23580
2
6月 16
10948
3
3月 15
9559
2
3月 15
4988
0
3月 15
3340