Skip to Content
Menu
This question has been flagged
2 Replies
7037 Views

I'm evaluating Odoo (v11) for use in our company and seem to be somewhat confused. If I do a csv import of my suppliers I can specify an External ID which is our current db's ID. But I can't seem to do that same thing via the API. Can I do this via the API? Here's a sample of my PHP code.

$id = $models->execute_kw(

    $db,

    $uid,

    $password,

    'res.partner', 'create',

    array(

        array(

            'id' => $record[0],

            'name' => $record[1],

            'supplier' => true,

            'customer' => false,

            'street' => $record[2],

            'street2' => $record[3],

            'city' => $record[4],

            'state_id' => $locality[0]['id'],

            'zip' => $record[6],

            'country_id' => $locality[0]['country_id'][0],

            'phone' => $record[7],

            'is_company' => true

        )

    )

);

Avatar
Discard
Best Answer

Hi Andrew - your question. I am trying to do the same also in php. Did you come across a solution for your question? Basically I want to keep a foreign_reference to the old record id so I can update it or create it.

Currently I am considering to alter that table to add a "_foreign_reference" field which will hold that old key so I can either create or update if the record is imported or not.

Also I am considering to do direct SQL queries into the database instead of utilizing the API. is this an accepted solution? For example I want to add or update a res_partner record. I could utilize the API or I could do a direct SQL query. If I do the direct SQL query, would something be missed? I am assuming that Odoo would probably fire some event when using the API. Or should I not worry?

Thank you kindly...

Avatar
Discard

Did you not understand my answer? Once you have imported the record, you will have the res_id of it - then just create the External ID in the ir_model_data table.

Hi and thank you for getting back to me. Yes I understand what you are saying and I want to do that, but I am confused how to go about this. Here is what I am thinking you are saying:

$odooRec = $models->execute_kw(

$db, $uid, $password,

'ir.model.data', 'search_read',

[

// What are the fields that I must search by in here??

]

);

if ($odooRec['id']){

$models->execute_kw(

$db, $uid, $password,

'res.partner', 'write',

[

[$odooRec['id']],

[

'name' => $oldRec['name'],

'supplier' => true,

...

]

]

);

}else{

$id = $models->execute_kw(

$db, $uid, $password,

'res.partner', 'create',

[

[

'id' => $id,

'name' => $oldRec['name'],

'supplier' => true,

...

]

]

);

$models->execute_kw(

$db, $uid, $password,

'ir.model.data', 'create',

[

[

// What fields must I write in here???

]

]

);

}

I am hoping you can see what my confusion is about. Thank you again for your reply...

Sorry not sure how to format the code in there...

Best Answer

I don't use PHP.

External ID's are not created unless you import or export data, so your newly created records won't have them.

In Python, you would create a dictionary containing the model, module, name and res_id (of the record you just created), then add a new entry in the ir_model_data model.

 


Avatar
Discard