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

Hi everyone!

I have this error:


psycopg2.errors.DatatypeMismatch: foreign key constraint "project_project_laboratorio_fkey" cannot be implemented
DETAIL:  Key columns "laboratorio" and "id" are of incompatible types: character varying and integer.


I have created a model "areas" (Table in database) to use it in a many2one field in a "project.project" inherited custom module I made. I loaded registers in my "areas" module in the python file.


  @api.model

  def init(self):

        default_data = [

            {'area_ejecutante': 'lie', 'laboratorio': 'lide'},

            {'area_ejecutante': 'ima', 'laboratorio': 'pintura'},

            {'area_ejecutante': 'lie', 'laboratorio': 'laie'},

            {'area_ejecutante': 'ima', 'laboratorio': 'mecanica'},

        ]

        for data in default_data:

        # Ensure no duplicates

            if not self.search([('area_ejecutante', '=', data['area_ejecutante']),

                                ('laboratorio', '=', data['laboratorio'])]):

                self.create(data)



Now I commented all the code and unlinked the "areas" model in the __init__.py . 

And I have this error.


Any idea how can I fix it?

Thanks in advance, Nicolás

Avatar
Discard

Many2one use _rec_name property and you need to have _compute_display_name method
P/s: can you mark my answer as best one please, thank in advance

Best Answer

No one create model record in the init method, i think you should use data.xml file or use post_init_hook please

Avatar
Discard
Author Best Answer

Hi Dương!

Thanks for your answer.


I think what you mentioned could be the problem. Do you have any doc or vid on how to create model records using data.xml?


I have reset the code to my last working commit but I still have this issue because the foreign key remains created. I'll proceed by deleting the foreign key directly through the database,

Avatar
Discard

How do you define your model and have you try new database ?

Author

Hi Dương,
I defined my module like this:

from odoo import api, models, fields

class AreasEjecutantesModel(models.Model):
_name = 'areas.ejecutantes.model'
_description = 'Modelo para gestionar las áreas ejecutantes y sus laboratorios'

# area_ejecutante1 = fields.Char(string='Area ejecutante1')
area_ejecutante1 = fields.Selection([
('lab1', 'lab_name'),
('lab2', 'lab_name'),
('lab3', 'lab_name'),
('lab4', 'lab_name')
], string="Area Ejecutante", default='lab1')
# laboratorio1 = fields.Char(string='Laboratorio1')
laboratorio1 = fields.Selection([
('sublab1','sublabname'),
('sublab2','sublabname'),
('sublab3','sublabname'),
('sublab4', 'sublabname'),
], string="Laboratorios", default='sublab1')

@api.model
def init(self):
default_data = [
{'area_ejecutante1': 'lab1', 'laboratorio1': 'sublab3'},
{'area_ejecutante1': 'lab2', 'laboratorio1': 'sublab1'},
{'area_ejecutante1': 'lab1', 'laboratorio1': 'sublab4'},
{'area_ejecutante1': 'lab2', 'laboratorio1': 'sublab2'},
]
for data in default_data:
# Ensure no duplicates
if not self.search([('area_ejecutante1', '=', data['area_ejecutante1']),
('laboratorio1', '=', data['laboratorio1'])]):
self.create(data)

By running this code the new table in the pgdb is created and the records are loaded in it.
I visualize the pgdb in pgAdmin

So your project.project model have m2o of your custom model right, in the __init__.py file you should your custom model first

Author

Exactly Dương, the project.project model have m2o of my custom model.

I did what you mentioned on the init.py and it solved the problem!! I now can visualize the model in the m2o field!

I have a question. If I want to make it dependent on the value selected in a Selection field, should I use the @api.onchange decorator and change the domain? Or is there another way to achieve this?

This is what I did, but it doesn't work.

@api.onchange('area_ejecutante')
def _onchange_area_ejecutante(self):
if self.area_ejecutante == 'lie':
return {'domain': {'laboratorio1': [('area_ejecutante1', '=', 'lie')]}}
elif self.area_ejecutante == 'ima':
return {'domain': {'laboratorio1': [('area_ejecutante1', '=', 'ima')]}}
else:
return {'domain': {'laboratorio1': []}}

Thank you!

You can use a compute field instead

Author

Hi Dương,
do you mean on the many2one field?
By trying to do so (adding the compute="myonchangefunction" on the m2o field) my module fails.

I would need that the options of the m2o field vary depending on the value of a Selection field.

e.g, in the selection field i would choose the laboratory, and m2o field should show the sublabs of the lab selected. Not all the sublabs of all the labs.

Thank you!

which odoo version?
You onchange function should assign field like this
if self.area_ejecutante1 == '':
self.field = ""

Author

Hi Dương,
I solved it by following Long's answer in this issue that you were also involved in: https://www.odoo.com/es_ES/forum/ayuda-1/issue-return-dynamic-domain-for-field-with-on-change-method-in-odoo-17-253508.

Thank you so much.

The only thing I need to finish now is how the information is displayed in my Many2one field. Currently, I see the format [modelname, id]. I read that the name_get method should be used, but I haven't been able to make it work yet.

Thank you again. Best regards,

Related Posts Replies Views Activity
0
Jul 24
1130
0
Feb 18
3213
2
Mar 15
8542
2
Dec 24
2552
1
Jun 23
2484