Skip to Content
Menu
This question has been flagged
1 Reply
2700 Views

Hi

I'm developing a module for Odoo where i have to import some data from external service and handle it internally. The imported data is related between them. I have defined the data models and developed the schema to import it from third party api.

I have defined the relation between data using Many2One relations, but the FK is defined using the field against the ID of the related model. As ID on Odoo are defined as Serial, it don't match with the right ID coming from external API. I have tried to define ID as integer and set the external API ID, but it gives me the error


TypeError: field 'id' cannot be assigned


There is any way that i can set a custom ID value to allow keep the external data relations?

Avatar
Discard
Author Best Answer

Hi community

I have been testing some variants to achieve the described goal, but there is not a simple way to set custom ID on Odoo, and the One2Many realtionship don't work form me without the direct Many2One relation.

I have solved this using triggers in PostgreSQL database. I have modified the models making some modifications to the generated model to inject the trigger. For example:

class Sample(models.Model):
_name = "sample"
_description = "Ejemplo"

rid = fields.Integer(
"ID de referencia", required=True, index=True, unique=True)
name = fields.Char("Nombre")
...

def init(self):
cr = self._cr
cr.execute('CREATE OR REPLACE FUNCTION id_sample() RETURNS trigger AS $id_sample$ '
' BEGIN '
' NEW.id := NEW.rid; '
' RETURN NEW; '
' END; '
' $id_sample$ LANGUAGE plpgsql; ')
cr.execute('DROP TRIGGER IF EXISTS trg_sample on sample')
cr.execute('CREATE TRIGGER trg_sample '
' BEFORE INSERT ON sample '
' FOR EACH ROW '
' EXECUTE PROCEDURE id_sample()')

I hope this can help other users.


Avatar
Discard
Related Posts Replies Views Activity
0
Oct 23
334
3
Apr 20
2399
1
Feb 20
1266
4
Mar 15
6493
1
Mar 15
3258