Skip to Content
Menu
This question has been flagged
3 Replies
7505 Views

In odoo 15, i got the issue after trying to create Many2many field in settings: 

psycopg2.errors.UndefinedTable: relation "product_product_res_config_settings_rel" does not exist 

My code: 


from odoo import api, fields, models
from ast import literal_eval

class SchoolSettings(models.TransientModel):
_inherit = 'res.config.settings'

product_default = fields.Many2many('product.product', string='Default Product')

@api.model
def get_values(self):
res = super(SchoolSettings, self).get_values()
if self.env['ir.config_parameter'].sudo().get_param('school.school_default_teacher'):
product_default = self.env['ir.config_parameter'].sudo().get_param('school.product_default')
res.update(
product_default=[(6, 0, literal_eval(product_default))]
)

return res


def set_values(self):
super(SchoolSettings, self).set_values()
self.env['ir.config_parameter'].set_param('school.product_default', self.product_default.ids)


Xml:


<div class="content-group">


    <label string="Default Product" for="product_default"/>


    <field name="product_default" widget="many2many_tags"/>


</div>



Please help, thanks.

Avatar
Discard
Author Best Answer

I've found the answer based on "Kiran K" answer:


from odoo import api, fields, models
from ast import literal_eval

class SchoolSettings(models.TransientModel):
_inherit = 'res.config.settings'

# many2many will give you multiselect options, when many2one only give you select option
# to use many2many, we must declare the middle table to connect 2 tables together.
# in this example, i create a new table named 'config_product_rel'
# with 2 column connect 2 table that is 'id' for the 'ir_config_parameter' table
# and 'product_id' for 'product_product' table
product_default_many2many = fields.Many2many('product.product', 'config_product_rel', 'id', 'product_id',
'Default Product Many2many')

@api.model
def get_values(self):
res = super(SchoolSettings, self).get_values()

# set values for many2many field
if self.env['ir.config_parameter'].sudo().get_param('school.product_default_many2many'):
product_default_many2many = self.env['ir.config_parameter'].sudo().get_param('school.product_default_many2many')
res['product_default_many2many'] = [(6, 0, literal_eval(product_default_many2many))]

return res


def set_values(self):
# get values for many2many field
self.env['ir.config_parameter'].set_param('school.product_default_many2many', self.product_default_many2many.ids)


Thanks.

Avatar
Discard
Best Answer

Hi  fudo,

Try, 

class ResCompany(models.Model):
_inherit = 'res.company'

def _active_products(self):
return self.env['product.product'].search([]).ids

product_ids = fields.Many2many('product.product', 'company_product_rel', 'company_id', 'product_id',
'Default Product',default=_active_products)


class ResConfigSettings(models.TransientModel):
_inherit = 'res.config.settings'

product_ids = fields.Many2many(related='company_id.product_ids', relation='product.product', readonly=False)

Hope it helps,
Kiran K


Avatar
Discard
Author

thanks you sir, but your answer is way too much for what i asking.

Author

I've post my answer above based your answer, thanks you for helping me.

Author

thanks you sir.

Best Answer

Lo definí así y funciono.

classProductWizard(models.TransientModel):

    _name = "product.wizard"

    _description = "See all Products and warehause"

    pricelist_ids = fields.Many2many(comodel_name='product.pricelist', string='Pricelist', required=True)

Avatar
Discard
Related Posts Replies Views Activity
0
Sep 22
961
1
Jul 22
1060
0
Feb 23
157
1
Jul 22
3571
0
Jul 22
1366