Ir al contenido
Menú
Se marcó esta pregunta
1 Responder
3775 Vistas

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
Descartar
Autor Mejor respuesta

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
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
0
oct 23
1269
3
abr 20
3490
1
feb 20
2201
4
mar 15
7570
1
mar 15
4242